Flutter 我在颤振中发现了类型错误。我该怎么办?我被困在这里了
如何解决颤振中的FindancestorStateof类型错误?我无法使用这些代码导航到其他页面。这个代码有什么问题 我得到的错误是 对null调用了方法“findAncestorStateOfType”。 收件人:空 已尝试调用:findAncestorStateOfType() 我的代码是:Flutter 我在颤振中发现了类型错误。我该怎么办?我被困在这里了,flutter,user-interface,visual-studio-code,project,user-experience,Flutter,User Interface,Visual Studio Code,Project,User Experience,如何解决颤振中的FindancestorStateof类型错误?我无法使用这些代码导航到其他页面。这个代码有什么问题 我得到的错误是 对null调用了方法“findAncestorStateOfType”。 收件人:空 已尝试调用:findAncestorStateOfType() 我的代码是: // entry point for the app, // the => operator is shorthand for {} when there is only one line of
// 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.*
//应用程序的入口点,
//当只有一行代码时,=>运算符是{}的简写
void main(){
runApp(材料应用程序)(
主页:HomeRoute(),
));
}
//我们应用程序的根部件
类HomeRoute扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
家:脚手架(
背景颜色:颜色(0xFFFAAC98),
appBar:appBar(
背景颜色:颜色(0xFFFAAC98),
),
正文:MyLayouWidget(),
),
);
}
}
//用下面示例中的代码替换此方法
小部件MyLayouWidget(){
返回容器(
子:列(
mainAxisAlignment:mainAxisAlignment.start,
儿童:[
填充物(
填充:常数边集全部(8.0),
子:容器(
宽度:300,
身高:50,
装饰:盒子装饰(
边界半径:边界半径。圆形(30.0),
颜色:常量颜色(0xff89C5CC),
),
儿童:中心(
儿童:新文本(
“Gram Panchayat应用程序”,
样式:TextStyle(
尺寸:27,
fontWeight:fontWeight.bold,
颜色:颜色(0xFF2F3676)),
),
),
),
),
划船(
儿童:[
填充物(
填充:仅限常量边集(右侧:18.0,顶部:18.0),
子:Image.asset(
“assets/images/human1.png”,
宽度:150,
身高:150,
),
),
填充物(
填充:仅限常量边集(左:58.0),
子项:elevatedButton(),
),
],
),
新行(
儿童:[
填充物(
填充:仅限常量边集(左:28.0),
子项:提升按钮1(),
),
填充物(
填充:仅限常量边集(顶部:58.0,左侧:68.0),
子:新建Image.asset(
“assets/images/human2.png”,
宽度:200,
身高:170,
),
),
],
),
填充物(
填充:仅限常量边集(左:60.0),
子对象:行(子对象:[
新形象资产(
“assets/images/img3.png”,
宽度:180,
身高:80,
),
]),
)
],
),
);
}
ElevatedButton ElevatedButton()=>ElevatedButton(
已按下:(){
构建语境;
导航器。推(
上下文
MaterialPartnerRoute(生成器:(上下文)=>SecondRoute()),
);
},
child:Text('Citizen'),
);
LevatedButton LevatedButton1()=>
提升按钮(按下时:(){},子项:Text('Staff');
类SecondRoute扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(“第二条路线”),
),
正文:中(
儿童:升降按钮(
已按下:(){
Navigator.pop(上下文);
},
child:Text('Go back!'),
),
),
);
}
}```
*请帮帮我,我卡住了。我浏览了很多网站,但都找不到问题所在,也找不到解决办法*
将MyLayouWidget()
放在HomeRoute
类中
和使用
Navigator.of(context,rootNavigator:true).pop();
而不是
Navigator.pop(context);