3

I know how to solve the problem but I don't why it's happening:

I've an app with 2 screens:

This main.dart:

import 'package:flutter/material.dart';
import 'HomeActivity.dart';

void main() {
  runApp(App());
}

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new HomeActivity();
  }
}

HomeActivity.Dart:

import 'package:flutter/material.dart';
import 'AddGameActivity.dart';

class HomeActivity extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Scaffold(
        appBar: AppBar(title: new Text("Home")),
        floatingActionButton: FloatingActionButton(
            onPressed: () => Navigator.push(context, new MaterialPageRoute(builder: (context) => new AddGameActivity())),
            child: new Icon(Icons.add))),
    );
  }

So here I've a screen with a FAB button to navigate me to AddGameActivity, when I press the FAB button this is the error message:

navigator operation requested with a context that does not include a Navigator

Now to solve this I added MaterialApp to main.dart and removed it from HomeActivity.dart like this:

main.dart:

import 'package:flutter/material.dart';
import 'HomeActivity.dart';

void main() {
  runApp(App());
}

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(home: HomeActivity());
    }
}

HomeActivity:

import 'package:flutter/material.dart';
import 'AddGameActivity.dart';

class HomeActivity extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: AppBar(title: new Text("Home")),
        floatingActionButton: FloatingActionButton(
            onPressed: () => Navigator.push(context, new MaterialPageRoute(builder: (context) => new AddGameActivity())),
            child: new Icon(Icons.add)));
  }
}

In that case when I add the Material app in main.dart it works correctly without any problem.

So my questions is WHY is this happening? in both of the ways I've a Material app which has a Scaffold inside !

Yousef Gamal
  • 1,026
  • 2
  • 17
  • 32
  • 1
    Check this excellent answer from Reimi: https://stackoverflow.com/a/51292613/666221 – diegoveloper Oct 29 '18 at 01:45
  • @diegoveloper So if I understood correctly: in that case in the 2nd example when I call HomeActivity() from main.dart, I'm passing the context from the MaterialApp in main.dart to build method in HomeAcitivty.dart right? – Yousef Gamal Oct 29 '18 at 04:31
  • 1
    yes, that's correct – diegoveloper Oct 29 '18 at 04:34
  • 1
    Okay great, Thank you very much @diegoveloper – Yousef Gamal Oct 29 '18 at 04:36
  • 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) – rmtmckenzie Oct 29 '18 at 14:35

1 Answers1

2

As mentioned in the comments, this issue seems to be a duplicate of this post: "Navigator operation requested with a context that does not include a Navigator"

The reason why the error is thrown is because the Navigator is unable to access the Navigator from the MaterialApp. To solve this issue, you can either declare MaterialApp and initialize HomeActivity - like what you're doing, or you can add a Builder after initializing MaterialApp inside HomeActivity.

import 'package:flutter/material.dart';

void main() {
  runApp(App());
}

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return HomeActivity();
  }
}

class HomeActivity extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      // call Builder to access Navigator from MaterialApp
      home: Builder(
        builder: (context) => Scaffold(
            appBar: AppBar(title: Text("Home")),
            floatingActionButton: FloatingActionButton(
                onPressed: () => Navigator.push(
                    context,
                    new MaterialPageRoute(
                        builder: (context) => AddGameActivity())),
                child: new Icon(Icons.add))),
      ),
    );
  }
}

class AddGameActivity extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
          appBar: AppBar(title: Text("AddGame")),
          floatingActionButton: FloatingActionButton(
              onPressed: () => Navigator.push(
                  context,
                  new MaterialPageRoute(
                      builder: (context) => new AddGameActivity())),
              child: new Icon(Icons.add)));
  }
}

Demo

Omatt
  • 8,564
  • 2
  • 42
  • 144