flutter - 我在 flutter 中遇到 findAncestorStateOfType 错误。我该怎么办?我被困在这里
问题描述
如何解决 Flutter 中的 findAncestorStateOfType 错误?我无法使用这些代码导航到其他页面。这段代码有什么问题?
我得到的错误是这个 方法'findAncestorStateOfType'在null上被调用。接收方:null 尝试调用:findAncestorStateOfType()
我的代码是这样的:
// entry point for the app,
// the => operator is shorthand for {} when there is only one line of code
void main() {
runApp(MaterialApp(
home: HomeRoute(),
));
}
// the root widget of our application
class HomeRoute extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: Color(0xFFFAAC98),
appBar: AppBar(
backgroundColor: Color(0xFFFAAC98),
),
body: myLayoutWidget(),
),
);
}
}
// replace this method with code in the examples below
Widget myLayoutWidget() {
return Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
width: 300,
height: 50,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30.0),
color: const Color(0xff89C5CC),
),
child: Center(
child: new Text(
'Gram Panchayat App',
style: TextStyle(
fontSize: 27,
fontWeight: FontWeight.bold,
color: Color(0xFF2F3676)),
),
),
),
),
Row(
children: [
Padding(
padding: const EdgeInsets.only(right: 18.0, top: 18.0),
child: Image.asset(
'assets/images/human1.png',
width: 150,
height: 150,
),
),
Padding(
padding: const EdgeInsets.only(left: 58.0),
child: elevatedButton(),
),
],
),
new Row(
children: [
Padding(
padding: const EdgeInsets.only(left: 28.0),
child: elevatedButton1(),
),
Padding(
padding: const EdgeInsets.only(top: 58.0, left: 68.0),
child: new Image.asset(
'assets/images/human2.png',
width: 200,
height: 170,
),
),
],
),
Padding(
padding: const EdgeInsets.only(left: 60.0),
child: Row(children: [
new Image.asset(
'assets/images/img3.png',
width: 180,
height: 80,
),
]),
)
],
),
);
}
ElevatedButton elevatedButton() => ElevatedButton(
onPressed: () {
BuildContext context;
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondRoute()),
);
},
child: Text('Citizen'),
);
ElevatedButton elevatedButton1() =>
ElevatedButton(onPressed: () {}, child: Text('Staff'));
class SecondRoute extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Second Route"),
),
body: Center(
child: ElevatedButton(
onPressed: () {
Navigator.pop(context);
},
child: Text('Go back!'),
),
),
);
}
}```
*Please help me I am stuck. I have gone through many sites but couldnt find what is wrong and the solution also.*
解决方案
myLayoutWidget()
在你的HomeRoute
班级里做
并使用
Navigator.of(context,rootNavigator:true).pop();
代替
Navigator.pop(context);
推荐阅读
- sql - 骡子软数据库选择查询与在哪里条件
- javascript - 如何垂直对齐CSS中的所有文本?
- unix - yum 缺少已安装的要求
- node.js - 使用 nexe 或 pkg 时忽略 settings.json 文件
- python - 使用 PyYAML 加载自定义对象
- python - 如何将外部对象传递给 YAML 日志记录配置文件?
- python - 在 Django ListAPIView 中设置 pagination_class 会导致运行时错误
- react-native - 部署的应用程序不反映本地发布测试?
- reactjs - Typescript/React 扩展默认功能组件道具给出错误
- c# - 运行时出现 FileNotFoundException(参考 .exe 文件进行调试)