- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我知道如何解决问题,但不知道为什么会这样:
我有一个有 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 按钮时,这是错误消息:
navigator operation requested with a context that does not include a Navigator
现在为了解决这个问题,我将 MaterialApp
添加到 main.dart
并将其从 HomeActivity.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());
}
}
首页事件:
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 应用程序
,其中有一个 Scaffold
!
最佳答案
如评论中所述,此问题似乎与这篇文章重复:"Navigator operation requested with a context that does not include a Navigator"
之所以报错是因为Navigator
无法从MaterialApp
访问Navigator
。要解决此问题,您可以声明 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)));
}
}
关于dart - 为什么 "Navigator operation requested with a context that does not include a Navigator",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53037529/
我是一名优秀的程序员,十分优秀!