我正在使用Flutter Streambuilder,我的流在加载之前调用null一秒钟。我正在尝试加载“正在加载...”卡,但它不起作用。在显示我想要的卡片列表之前,我的模拟器会短暂显示一个红色错误屏幕我怎么能让它停止这样做?这是我的代码......
new Expanded(
child: new StreamBuilder(
stream: streamQuery,
builder:
(BuildContext context, AsyncSnapshot<Event> event) {
if (event.data.snapshot.value == null) {
return new Card(
child: new Text('Loading...',
style: new TextStyle(
fontSize: 12.0,
fontWeight: FontWeight.bold,
fontStyle: FontStyle.italic)),
);
}
Map myMap = event.data.snapshot.value; //store each map
var titles = myMap.values;
List onesTitles = new List();
List onesIds = new List();
List onesImages = new List();
List onesRank = new List();
for (var items in titles) {
onesTitles.add(items['vidTitle']);
onesIds.add(items['vidId']);
onesImages.add(items['vidImage']);
onesRank.add(items['Value']);
}
names = onesTitles;
ids = onesIds;
numbers = onesRank;
vidImages = onesImages;
switch (event.connectionState) {
case ConnectionState.none:
return new Card(
child: new Text('Loading...',
style: new TextStyle(
fontSize: 12.0,
fontWeight: FontWeight.bold,
fontStyle: FontStyle.italic)),
);
case ConnectionState.waiting:
return new Card(
child: new Text('Awaiting Results...',
style: new TextStyle(
fontSize: 12.0,
fontWeight: FontWeight.bold,
fontStyle: FontStyle.italic)),
);
default:
if (event.hasError)
return new Card(
child: new Text('Error: ${event.error}',
style: new TextStyle(
fontSize: 12.0,
fontWeight: FontWeight.bold,
fontStyle: FontStyle.italic)),
);
else
return new InkWell(
答案 0 :(得分:2)
您需要明确说明else
内具有if (snapshot!=null) {//do something}
这样的事情:
else //event.data.snapshot.value != null
{
Map myMap = event.data.snapshot.value; //store each map
var titles = myMap.values;
List onesTitles = new List();
List onesIds = new List();
List onesImages = new List();
List onesRank = new List();
for (var items in titles) {
onesTitles.add(items['vidTitle']);
onesIds.add(items['vidId']);
onesImages.add(items['vidImage']);
onesRank.add(items['Value']);
}
names = onesTitles;
ids = onesIds;
numbers = onesRank;
vidImages = onesImages;
//return my layout
}
所以不要在snapshot.value
之后的if语句之外留下任何使用snapshot!=null
的内容。