2017-08-20 78 views
0

我收到我的Flutter應用程序的Navigator operation requested with a context that does not include a Navigator.錯誤日誌。我看到的所有例子都是針對「無狀態」小部件的,所以我認爲這可能是問題或我的代碼?我怎麼路由到我的LoginPage?這裏是我的代碼沒有任何導航或路線...如何爲Stateful Widget創建Flutter路由?

void main() { 
     runApp(new MyApp()); 
    } 

    final googleSignIn = new GoogleSignIn(); 
    final fb = FirebaseDatabase.instance.reference(); 
    final auth = FirebaseAuth.instance; 

    class MyApp extends StatefulWidget { 
     @override 
     _MyAppState createState() => new _MyAppState(); 
    } 

    class _MyAppState extends State<MyApp> { 
     String _platformVersion = 'Unknown'; 

     @override 
     initState() { 
     super.initState(); 
     initPlatformState(); 
     } 

     // Platform messages are asynchronous, so we initialize in an async method. 
     initPlatformState() async { 
     String platformVersion; 
     // Platform messages may fail, so we use a try/catch PlatformException. 
     try { 
      platformVersion = await Myfavkpopflutter.platformVersion; 
     } on PlatformException { 
      platformVersion = 'Failed to get platform version.'; 
     } 

     // If the widget was removed from the tree while the asynchronous platform 
     // message was in flight, we want to discard the reply rather than calling 
     // setState to update our non-existent appearance. 
     if (!mounted) return; 

     setState(() { 
      _platformVersion = platformVersion; 
     }); 
     } 

     void MyFavAction() { 
     setState(() { 
      print("MYFAV"); 

    //  fb.child("messages").orderByValue().onChildAdded.listen((Event event) { 
    //  print('Child added: ${event.snapshot.value}'); 
    //  }); 
     }); 
     } 

     void SearchAction() { 
     setState(() { 
      print("Search"); 
     }); 
     } 



     @override 
     Widget build(BuildContext context) { 
     if (auth.currentUser == null) { 
      return new MaterialApp(
      routes: <String, WidgetBuilder>{ 
      '/settings': (BuildContext context) => new LoginPage(), 
      }, 
      home: new Scaffold(
       backgroundColor: Colors.white70, 
       appBar: new AppBar(
       title: new Text(
        "MyFavKPop", 
        style: new TextStyle(
         color: Colors.black, 
         fontWeight: FontWeight.bold, 
         fontSize: 25.00), 
       ), 
       backgroundColor: Colors.amber, 
      ), 
       body: new Container(
        child: new Center(
         child: new Column(
       mainAxisAlignment: MainAxisAlignment.center, 
       children: <Widget>[ 
        new RaisedButton(
        onPressed: MyFavAction, 
        color: Colors.lightBlue, 
        elevation: 20.00, 
        splashColor: Colors.amber, 
        child: new Text(
         "MyFav KPOP", 
         style: new TextStyle(color: Colors.black, fontSize: 20.00), 
        ), 
       ), 
        new Padding(padding: new EdgeInsets.all(30.00)), 
        new RaisedButton(
        onPressed: SearchAction, 
        color: Colors.lightBlue, 
        elevation: 20.00, 
        splashColor: Colors.amber, 
        child: new Text(
         "MyFav SEARCH", 
         style: new TextStyle(color: Colors.black, fontSize: 20.00), 
        ), 
       ), 
        new Padding(padding: new EdgeInsets.all(30.00), 
       ), 
        new RaisedButton(
        onPressed: SearchAction, 
        elevation: 20.00, 
        splashColor: Colors.amber, 
        color: Colors.lightBlue, 
        child: new Text(
         "MyFav FRIENDS", 
         style: new TextStyle(color: Colors.black, fontSize: 20.00), 
        ), 
       ), 
        new Padding(
        padding: new EdgeInsets.all(30.00), 
       ), 
        new RaisedButton(
        onPressed: SearchAction, 
        elevation: 20.00, 
        splashColor: Colors.amber, 
        color: Colors.lightBlue, 
        child: new Text(
         "MyFav CHAT", 
         style: new TextStyle(color: Colors.black, fontSize: 20.00), 
        ), 
       ), 
        new Padding(
        padding: new EdgeInsets.all(30.00), 
       ), 
        new RaisedButton(
        onPressed: SearchAction, 
        elevation: 20.00, 
        splashColor: Colors.amber, 
        color: Colors.lightBlue, 
        child: new Text(
         "MyFav #1'S", 
         style: new TextStyle(color: Colors.black, fontSize: 20.00), 
        ), 
       ) 
       ], 
      ))), 
       floatingActionButton: new FloatingActionButton(
        child: new Text(
        "Log Out", 
        style: new TextStyle(fontWeight: FontWeight.bold, fontSize: 12.00), 
       ), 
        onPressed:() => Navigator.of(context).pushNamed('/settings'),, 
      ), 
     )); 
     } 
     } 
    } 

    class LoginPage extends StatelessWidget { 
    @override 
    Widget build(BuildContext context) { 
    return new Scaffold(
     appBar: new AppBar(
     title: new Text("Login/Signup"), 
    ), 
     body: new Container(
     child: new Center(
      child: new Column(
      mainAxisAlignment: MainAxisAlignment.center, 
      children: <Widget>[ 
       new TextField(
       decoration: new InputDecoration(
        hintText: "E M A I L A D D R E S S"), 
      ), 
       new Padding(padding: new EdgeInsets.all(15.00)), 
       new TextField(
       obscureText: true, 
       decoration: 
       new InputDecoration(hintText: "P A S S W O R D"), 
      ), 
       new Padding(padding: new EdgeInsets.all(15.00)), 
       new TextField(
       decoration: 
       new InputDecoration(hintText: "U S E R N A M E"), 
      ), 
       new RaisedButton(
       onPressed: null, 
       child: new Text("SIGNUP"), 
      ), 
       new Padding(padding: new EdgeInsets.all(15.00)), 
       new RaisedButton(
       onPressed: null, 
       child: new Text("LOGIN"), 
      ), 
       new Padding(padding: new EdgeInsets.all(15.00)), 
       new RaisedButton(
       onPressed: null, 
       child: new Text("Facebook"), 
      ), 
       new Padding(padding: new EdgeInsets.all(5.00)), 
       new RaisedButton(
       onPressed: null, 
       child: new Text("Google"), 
      ) 
      ], 
     ), 
     ), 
     margin: new EdgeInsets.all(15.00), 
    ), 
    ); 
    } 
} 

****編輯以下是錯誤日誌中我得到...

══╡異常捕獲手勢╞══ ══════════════════════════════════════════════════ ═══════════════在處理手勢 以下斷言被拋出:與不包括導航上下文請求 導航操作。 用於從導航器中推送或彈出路由的上下文必須是作爲導航器小部件的後代的小部件的上下文。

════════════════════════════════════════ ══════════════════════════════════════════════════ ════

回答

1

你在你的問題是導航到StatefulWidget而在您的文章你是問如何導航到StatelessWidget LoginPage標題中所述。

無論如何,你應該能夠實現導航以同樣的方式,不管國家的事情。

儘量遵循這是從一個應用程序,我的工作就採取了以下流程。

//Local Imports 
//MyTabs.dart is where I define my home page which is a StatefulWidget with TabBarView 
import 'MyTabs.dart' as first; 

.....

//My main function where I start my app 

    void main() { 
     runApp(new MaterialApp(
     home: new SignIn(), 
     routes: <String, WidgetBuilder>{ 
      "/mytabs" : (BuildContext context)=> new first.MyTabs(), 
      //add more routes here 
     }, 

    )); 
    } 

我的應用程序與登錄頁面開始,我創建了一個簡單的人比我必須拿出主意

class SignIn extends StatelessWidget { 
    @override 
    Widget build(BuildContext context) { 
    return new Scaffold(
    appBar: new AppBar(
     title: new Text("Sign In"), 
    ), 
    body: new IconButton(icon: new Icon(Icons.arrow_forward), onPressed:(){Navigator.of(context).pushNamed("/mytabs");}), 
) 

我希望我有什麼幫助,試着在你的代碼中遵循這個流程,讓我知道它是怎麼回事。

+0

明白了。當我將我的路線放入我的'void main()'而不是在我最初的家庭班級中時,它工作正常。 –

0

此錯誤無關,與狀態或無狀態的路線。你可以看我的回答here作爲一個工作示例。

順便說一句:我無法找到你的代碼你對導航儀的呼叫。你如何以及在哪裏打電話推?

編輯

你應該總是隻有一個MaterialApp在您的應用程序。您可以爲一個MaterialApp提供各種路線(參見我的示例)。

+0

我已經添加了破損的代碼,並根據您的示例進行了一些更改,但仍然出現錯誤。請查閱。 –