为什么使用不包含导航器的上下文请求了导航器操作

时间:2018-10-29 01:07:23

标签: dart flutter navigator

我知道如何解决问题,但我不知道为什么会发生:

我有一个带有2个屏幕的应用程序:

此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))),
    );
  }

因此,这里有一个带有FAB按钮的屏幕,可将我导航到AddGameActivity,当我按FAB按钮时,这是错误消息:

  

在不包含导航器的上下文中请求的导航器操作

现在要解决此问题,我将MaterialApp添加到main.dart并将其从HomeActivity.dart中删除,如下所示:

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)));
  }
}

在这种情况下,当我在main.dart中添加Material应用程序时,它可以正常工作而没有任何问题。

所以我的问题是为什么会这样?在这两种方式中,我都有一个Material app,里面有一个Scaffold

1 个答案:

答案 0 :(得分:0)

如评论中所述,此问题似乎与该帖子重复:"Navigator operation requested with a context that does not include a Navigator"

引发错误的原因是因为Navigator无法从Navigator访问MaterialApp。要解决此问题,您可以声明MaterialApp并初始化HomeActivity(如您所做的那样),也可以在HomeActivity中初始化MaterialApp之后添加Builder

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