问题
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