android-studio - Flutter 错误:列的子项不得包含任何空值,但在索引 0 处找到空值
问题描述
我在 android studio 中创建了一个应用程序来从一个屏幕导航到另一个屏幕。这里将两个无状态小部件创建为两个屏幕,并且都包含一个按钮来相互导航页面。但是,当我运行该应用程序时,我的 android 手机上会生成一个红屏,我收到一条错误消息
exception 'Column's children must not contain any null values, but a null value was found at index 0'.
我在下面提供了我的代码:
第一个屏幕
class FirstScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("First Screen"),
),
body: Container(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
center(
decoration: new BoxDecoration(
image: new DecorationImage(
image: new AssetImage('assets/new 7wonders.jpg'),
fit: BoxFit.cover,
),
),
),
Text('New 7 Wonders',
style: TextStyle(fontSize: 40, fontStyle: FontStyle.italic),
),
RaisedButton(
child: Text("Bang Here"),
onPressed: (){
Navigator.push(context, MaterialPageRoute(builder: (context) => SecondScreen()));
},
color: Colors.red,
textColor: Colors.yellow,
padding: EdgeInsets.fromLTRB(10, 10, 10, 10),
splashColor: Colors.grey,
)
],
),
),
),
);
}
center({BoxDecoration decoration}) {}
}
第二个屏幕
class SecondScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Second Screen"),
),
body: RaisedButton(
child: Text("Go to First page"),
onPressed:() {
Navigator.pop(context);
},
),
);
}
}
解决方案
您的center
方法应该返回一个小部件,它当前提供null
给Column
.
改为这样做:
Widget center() {
// return a decorated box widget which gives you the decoration property
return Image(
image: AssetImage(
'assets/new 7wonders.jpg',),
fit: BoxFit.cover,
);
}
}
然后在你Column
喜欢的地方使用:
Container(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// call the center method which returns a Widget
center(),
Text(
'New 7 Wonders',
style: TextStyle(fontSize: 40, fontStyle: FontStyle.italic),
),
RaisedButton(
child: Text("Bang Here"),
onPressed: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) => SecondScreen()));
},
color: Colors.red,
textColor: Colors.yellow,
padding: EdgeInsets.fromLTRB(10, 10, 10, 10),
splashColor: Colors.grey,
)
],
),
),
),
推荐阅读
- java - java中三个数字相加的问题
- arrays - 使用 v-for 在数组中显示数组的正确方法
- ftp - 在批处理脚本中从 WinSCP 环境中捕获 FTP 连接错误代码
- cassandra - 是否可以在 Cassandra 中获得一系列时间戳之间的平均时间?
- algorithm - 以下两种字符串置换算法的实现在时间和空间复杂度方面哪个更好?
- css - 当浏览器较小时,Bootstrap NavBar 中的徽标将汉堡包向左移动
- c++ - 在 C/C++ 的多维数组中是否有一种计算上有效的方法来存储小于一个字节的值?
- reactjs - 用方法反应 Jest 测试
- java - 是什么使按钮背景变白,它不应该是透明的吗?
- c# - 为什么我的 .NET 应用程序不会“加载文件或程序集‘HDF5DotNet’”?