简体   繁体   English

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

[英]WHY "Navigator operation requested with a context that does not include a Navigator"

I know how to solve the problem but I don't why it's happening:我知道如何解决问题,但我不知道为什么会这样:

I've an app with 2 screens:我有一个有 2 个屏幕的应用程序:

This main.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 HomeActivity();
  }
}

HomeActivity.Dart: 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:所以这里我有一个带有 FAB 按钮的屏幕,可以将我导航到AddGameActivity ,当我按下 FAB 按钮时,这是错误消息:

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:现在为了解决这个问题,我将MaterialApp添加到main.dart并将其从HomeActivity.dart删除,如下所示:

main.dart:主要.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.在这种情况下,当我在main.dart添加 Material 应用程序时,它可以正常工作,没有任何问题。

So my questions is WHY is this happening?所以我的问题是为什么会这样? in both of the ways I've a Material app which has a Scaffold inside !在这两种方式中,我都有一个Material app ,里面有一个Scaffold

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 .之所以抛出错误,是因为Navigator无法从MaterialApp访问Navigator 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.为了解决这个问题,您可以声明 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)));
  }
}

演示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用不包含 Navigator 的上下文请求的 Navigator 操作。(Flutter) - Navigator operation requested with a context that does not include a Navigator.(Flutter) 在从内存中恢复应用程序时显示密码屏幕 [使用不包含导航器的上下文请求的导航器操作。] - show pincode screen on app resume from memory [Navigator operation requested with a context that does not include a Navigator.] 如何修复使用不包含导航器的上下文请求的导航器操作。 ' 在 flutter - How to fix 'Navigator operation requested with a context that does not include a Navigator. ' in flutter Flutter 使用不包括导航器的上下文请求导航器操作。 在启动画面上 - Flutter Navigator operation requested with a context that does not include a Navigator. on splash screen 使用不包含 Navigator 的上下文请求的 Navigator 操作。 (生成器 function 不会显示 MainPage()) - Navigator operation requested with a context that does not include a Navigator. (Builder function wont display MainPage()) StatefulWidget:使用上下文请求的导航器操作 - StatefulWidget: Navigator operation requested with a context 在 Flutter 上出现错误 - 请求导航器操作 - Get Error on Flutter - Navigator operation requested Flutter 小部件测试:使用不包含 AutoRouter 的上下文请求 AutoRouter 操作 - Flutter Widget Test: AutoRouter operation requested with a context that does not include an AutoRouter 请求了不包括本地化祖先的上下文的语言环境 - Requested the Locale of a context that does not include a Localizations ancestor Flutter 不做导航器 - Flutter does not make the Navigator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM