Mandel
Mandel

Reputation: 223

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

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.

Upvotes: 0

Views: 2714

Answers (2)

Mandel
Mandel

Reputation: 223

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.

Upvotes: 0

Murat Aslan
Murat Aslan

Reputation: 1580

try this one

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

Upvotes: 1

Related Questions