我正在尝试从Firestore提取数据,但是我一直在空错误时调用method 我希望它在出现错误或连接缓慢的情况下继续显示循环进度指示器
class FirstTabPage extends StatelessWidget {
final CustomCard customCard = CustomCard();
@override
Widget build(BuildContext context) {
return
Padding(
padding: const EdgeInsets.all(8.0),
child: StreamBuilder(
stream: Firestore.instance.document('data/Sqprices').snapshots(),
builder: (context, snapshot){
if(!snapshot.hasData){
return Center(child: CircularProgressIndicator(backgroundColor: Colors.black,),);
} else {
return ListView(
children: <Widget>[
CustomCard(
image: Image.asset('images/elag.png',width: 40,height: 40,),
currencyName: 'EUR',
buyPrice: '${snapshot.data['price1']}',
sellPrice: '${snapshot.data['price2']}',
),
CustomCard(
image: Image.asset('images/ulag.png',width: 40,height: 40,),
currencyName: 'USD',
buyPrice: '${snapshot.data['price3']}',
sellPrice: '${snapshot.data['price4']}',
),
CustomCard(
image: Image.asset('images/klag.png',width: 40,height: 40,),
currencyName: 'GBP',
buyPrice: '${snapshot.data['price5']}',
sellPrice: '${snapshot.data['price6']}',
),
],
);
}
}
),
);
}
}
有Internet访问时,进度指示器将显示2秒钟,然后显示数据,但是在Internet访问缓慢时,崩溃将提示 方法[]在null上被调用
答案 0 :(得分:0)
尝试一下,
builder: (context, AsyncSnapshot snapshot){
......
.....
....
}
答案 1 :(得分:0)
您已检查.hasData
,以确保流中的数据可用。但是,Stream中的数据也可以是空数据。因此,您应该检查数据是否等于null:
child: StreamBuilder(
stream: Firestore.instance.document('data/Sqprices').snapshots(),
builder: (context, snapshot){
if(!snapshot.hasData){
return Center(child: CircularProgressIndicator(backgroundColor: Colors.black,),);
}
if(snapshot.data == null) return Center(child: Text("No data"));
else {
return ListView(