0

I'm trying to navigate to another screen. However, I'm getting:

Another exception was thrown: Navigator operation requested with a context that does not include a Navigator

I found a solution on GitHub (https://github.com/flutter/flutter/issues/15919) by @burhanrashid52 on how to solve this. But, the example he uses is using a StatelessWidget. Can somebody give me an example on how to solve this with a StatefulWidget? Thanks.

class ToDoState extends State<ToDo> {
  @override
  Widget build (BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        floatingActionButton: FloatingActionButton(
          backgroundColor: Color(0xff00bbff),
          child: Icon(
            Icons.chat_bubble
          ),
          onPressed: () {
            Navigator.push(context, MaterialPageRoute(builder: (context) => NewTask()));
            print("test");
          }
        ),
        body: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            SafeArea(
              child: Container(
                child: Padding(
                  padding: EdgeInsets.fromLTRB(20, 40, 20, 20),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      Text(
                        "Task List",
                        style: TextStyle(
                          fontWeight: FontWeight.w800,
                          fontSize: 22,
                          color: Color(0xff242424)
                        ),
                      ),
                      Text(
                        "1 incomplete",
                        style: TextStyle(
                          fontWeight: FontWeight.w400,
                          fontSize: 14,
                          color: Color(0xffb3b3b3)
                        ),
                      ),
                    ],
                  )
                ),
              ),
            ),
            Expanded(
              child: ListView(
                padding: EdgeInsets.zero,
                children: <Widget>[
                  Item("Get shampoo from the store", "Today at 3:49pm", true),
                  Item("Go to college class", "Today at 3:49pm", true),
                  Item("Work on the ToDo app", "Today at 5:27pm", false),
                  Item("Test this app", "Today at 3:49pm", true),
                ],
              )
            )
          ],
        ),
      ),
    );
  }


  [1]: https://github.com/flutter/flutter/issues/15919
Jonathon
  • 1,491
  • 2
  • 13
  • 21
  • Possible duplicate of [Navigator operation requested with a context that does not include a Navigator](https://stackoverflow.com/questions/44004451/navigator-operation-requested-with-a-context-that-does-not-include-a-navigator) – Logemann Nov 17 '19 at 23:04

1 Answers1

1

Surround your FloatingActionButton with a Builder widget

return MaterialApp(
  home: Scaffold(
    floatingActionButton: Builder(builder: (context)=> FloatingActionButton(
      backgroundColor: Color(0xff00bbff),
      child: Icon(
        Icons.chat_bubble
      ),
      onPressed: () {
        Navigator.of(context).push(MaterialPageRoute(builder: (context) => new NewTask()));
        print("test");
      }
    ),),
    body: Column(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        SafeArea(
          child: Container(
            child: Padding(
              padding: EdgeInsets.fromLTRB(20, 40, 20, 20),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Text(
                    "Task List",
                    style: TextStyle(
                      fontWeight: FontWeight.w800,
                      fontSize: 22,
                      color: Color(0xff242424)
                    ),
                  ),
                  Text(
                    "1 incomplete",
                    style: TextStyle(
                      fontWeight: FontWeight.w400,
                      fontSize: 14,
                      color: Color(0xffb3b3b3)
                    ),
                  ),
                ],
              )
            ),
          ),
        ),
        Expanded(
          child: ListView(
            padding: EdgeInsets.zero,
            children: <Widget>[

            ],
          )
        )
      ],
    ),
  ),
);
  • 1
    right answer. Even though this question was posted (and answered) on Stackoverflow like 144 times already ;-) – Logemann Nov 17 '19 at 23:03