How to use routes in flutter to navigate to page other than from main?

断了今生、忘了曾经 提交于 2021-02-20 04:12:50

问题


I created a route for navigating from one page to another, in the following way

class task extends StatelessWidget{
  @override
  Widget build(BuildContext context) {

    return MaterialApp(
        title: 'Task',
        home: new task(),
        routes: <String, WidgetBuilder>{
          "/Completed": (BuildContext context) => new Completed()

        }
    );
  }
}


class taskScreen extends StatefulWidget{

  @override
  taskState createState() => new taskState();
}


class taskState extends State<taskScreen> {

  @override
  Widget build(BuildContext context) {


    Column taskScreen = Column(
        children: <Widget>[
              FlatButton(
              ..,
              onPressed: (){
                Navigator.of(context).pushNamed("/Completed");  
               },
              child: Text(
              "Completed",
              ),
              ),
            ],
          )
        ]);
    return Scaffold(
      appBar: AppBar(
          title: Text('Task Screen')),
      body: taskScreen,
    );
  }

}

However when i try navigating it gives the error :

Could not find a generator for route RouteSettings("/Completed", null) in the _WidgetsAppState.

How can I fix this error?

I have used implemented route before from my main.dart page to the second page which worked properly however its not working here.


回答1:


try this one

Navigator.pushNamed(context, "/Completed");  



回答2:


I figured out my error. Instead of instantiating task(),i had created an instance of taskScreen(). Hence the new route was never set. It solved my issue but I'm still curious as to what difference it made as both of them gave the same output Screen.



来源:https://stackoverflow.com/questions/56233077/how-to-use-routes-in-flutter-to-navigate-to-page-other-than-from-main

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!