简体   繁体   English

如何为有状态小工具创建Flutter路由?

[英]How to create Flutter route for Stateful Widget?

I'm getting the Navigator operation requested with a context that does not include a Navigator. 我正在Navigator operation requested with a context that does not include a Navigator. error log for my Flutter app. Flutter应用的错误日志。 All the examples I see are for "Stateless" widgets so I think this may be the issue or my code? 我看到的所有示例都是针对“无状态”小部件的,所以我认为这可能是问题所在还是我的代码? How can I route to my LoginPage ? 如何路由到我的LoginPage Here is my code without any Navigator or route... 这是我的代码,没有任何导航器或路线...

 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 Here is the error log I'm getting... ****编辑这是我得到的错误日志...

══╡ EXCEPTION CAUGHT BY GESTURE ╞═══════════════════════════════════════════════════════════════════ The following assertion was thrown while handling a gesture: Navigator operation requested with a context that does not include a Navigator. BY手势引起的异常╞══════════════════════════════════════════ handling处理手势时引发了以下断言:请求的导航器操作使用了不包含导航器的上下文。 The context used to push or pop routes from the Navigator must be that of a widget that is a descendant of a Navigator widget. 用于从导航器推送或弹出路由的上下文必须是作为导航器小部件的后代的小部件的上下文。

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

You stated in the title that your problem is navigating to a StatefulWidget while in your post you are asking about how to navigate to a StatelessWidget LoginPage. 您在标题中指出,问题正在导航到StatefulWidget,而在您的帖子中,您正在询问如何导航到StatelessWidget LoginPage。

Anyhow, you should be able to implement navigating the same way regardless of the state matter. 无论如何,无论状态如何,您都应该能够以相同的方式实现导航。

Try to follow the following flow which is taken from an app I am working on. 尝试遵循以下流程,该流程取自于我正在开发的应用程序。

//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
        },

      ));
    }

My app starts with a SignIn page, I have created a simpler one than I have to show the idea 我的应用程序从“登录”页面开始,我创建了一个比演示页面更简单的应用程序

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

I hope I was of any help, try to follow this flow in your code and let me know how it went. 希望对您有所帮助,请尝试在您的代码中遵循此流程,并告诉我它的运行方式。

Navigator.of(context).push(new MaterialPageRoute(builder: 
                   (BuildContext context) => new MyTabs()));

For navigation, we can use the above code also. 对于导航,我们也可以使用上面的代码。

This error has nothing to do with stateful or stateless routes. 此错误与有状态或无状态路由无关。 You can look at my answer here for a working example. 您可以在这里查看我的答案,以获取有效的示例。

By the way: I can't find your call to Navigator in your code. 顺便说一句:我在您的代码中找不到您对Navigator的调用。 How and where do you call push? 您如何以及在哪里称呼推送?

edit 编辑

You should always only have one MaterialApp in your applications. 您的应用程序中始终应该只有一个MaterialApp。 You can provide various routes to one MaterialApp (see my example). 您可以提供到一个MaterialApp的各种路由(请参见我的示例)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM