2017-08-20 3 views
0

FlutterアプリのエラーログがNavigator operation requested with a context that does not include a Navigator.になっています。私が見ているすべての例は、「ステートレス」ウィジェットのためのものなので、これが問題または私のコードかもしれないと思いますか? LoginPageにルーティングするにはどうすればよいですか?ここで任意のナビゲーターまたはルートのない私のコードは...あるステートフルウィジェット用フラッタールートの作成方法

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), 
    ), 
    ); 
    } 
} 

**** EDITここで私が得ているエラーログ...ジェスチャーでキャッチ

══╡例外はある╞══ == == == == == == == == == == == == == == === == == == == == == == = ==== == == == == == === ジェスチャーの処理中に、次のアサーションがスローされました。 ナビゲーターが含まれていないコンテキストでナビゲーター操作が要求されました。 ナビゲータからルートをプッシュまたはポップするコンテキストは、ナビゲータウィジェットの子孫である のウィジェットである必要があります。

== == == == == == == === == == == == == == == == == == == = == == == == == == == == == == == == == == === == == == == == == == = ==== =

答えて

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");}), 
) 

を表示するために持っているよりも1単純に作成していますどのような助けであっても、あなたのコードでこの流れに従ってみて、それがどのように行ったのか教えてください。

+0

入手しました。私のルートを最初のホームクラスの代わりに 'void main()'に置いたときに働いた。 –

0

このエラーは、ステートフルルートまたはステートレスルートとは何の関係もありません。あなたは私の答えhereの実例を見ることができます。

ところで、あなたのコードでNavigatorへの呼び出しを見つけることができません。どのようにプッシュを呼びますか?あなたはいつも、あなたのアプリケーションで1 MaterialAppを持っている必要があり

編集

。 1つのMaterialAppにさまざまなルートを提供できます(私の例を参照)。

+0

私は壊れたコードを追加して、あなたの例にいくつかの変更を加えましたが、それでもエラーを投げかけています。レビューしてください。 –

関連する問題