这是错误:
有问题的小部件是:FutureBuilder
构建函数绝不能返回 null。
要返回导致建筑小部件填充可用空间的空白空间,请返回“Container()”。要返回占用尽可能少空间的空白空间,请返回“Container(width: 0.0, height: 0.0)”。
这是获取 Firebase 实时数据的函数:
Future<void> getCategoriesName() async {
await FirebaseDatabase.instance.reference().child('Categories').once()
.then((DataSnapshot dataSnapshot){
var key = dataSnapshot.value.keys;
for(var i in key)
{
CategoryItems categoryItems = new CategoryItems(
dataSnapshot.value[i]['CategoryName'],
dataSnapshot.value[i]['Counter']
);
categoryItemList.add(categoryItems);
}
setState(() {
print(categoryItemList.length);
});
});
}
这是我的未来构建器功能:
Container(
margin: EdgeInsets.only(bottom: 50,top: 100),
child: FutureBuilder(
future: FirebaseDatabase.instance.reference().child('Categories').child(_userPin).once(),
// ignore: missing_return
builder: (context, snapshot) {
if(snapshot.hasData){
if (snapshot.data!=null) {
return Expanded();
}else{
return Loader();
}
}
}
),
),
在 Expanded 内部,我有 ListView.builder,仅当我的数据加载时,上述错误才会抛出几秒钟,它会在屏幕上显示项目。我在 FutureBuiler 的容器之上有 Stack 小部件。
看来您没有从以下位置返回任何内容builder
回调函数,当snapshot.hasData
是假的。这解释了为什么在从数据库加载数据之前您只会短暂地看到错误。你可以返回一个CircularProgressIndicator
小部件来自builder
当您等待数据从数据库到达时的回调函数。你也可以摆脱// ignore: missing_return
评论因为builder
回调函数应该始终返回一个小部件。
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Container(
margin: EdgeInsets.only(bottom: 50, top: 100),
child: FutureBuilder(
future: FirebaseDatabase.instance.reference().child('Categories').child(_userPin).once(),
builder: (context, snapshot) {
if (snapshot.hasData) {
if (snapshot.data != null) {
return Expanded();
} else {
return Loader();
}
} else {
return CircularProgressIndicator();
}
},
),
),
),
);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)