这是错误:
令人讨厌的窗口小部件是:FutureBuilder构建函数绝不能返回null。要返回使建筑物小部件填充可用空间的空白区域,请返回“ Container()”。要返回占用尽可能少空间的空白空间,请返回“ Container(width:0.0,height:0.0)”。
这是获取Firebase Realtime数据的功能:
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);
});
});
}
这是我的Future Builder函数:
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();
}
}
}
),
),
在展开后的ListView.builder内部,上述错误仅在加载数据时才显示几秒钟,并显示带有项目的屏幕。我在FutureBuiler容器的顶部有堆栈小部件。
看起来为假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();
}
},
),
),
),
);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句