这是我的流生成器的代码。
StreamBuilder<RatesData>(
stream: DatabaseRatesService(fid: fid).ratesData,
builder: (context, snapshot) {
if (!snapshot.hasData) // Doesn't get off from this condition and keeps loading.
return LoadingLogin(); // A simple loading widget
RatesData ratesData = snapshot.data;
print(ratesData.fid);
return Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Container(
height: 50,
child: Text(
ratesData.fid,
style: TextStyle(color: Colors.black),
),
),
],
);
}
)
这是我进行所有数据库查询的类。我似乎已成功从Firebase获取数据,但未在流构建器中显示该数据。我怀疑数据没有注入到我创建的数据模型中,但是不知道如何。
class DatabaseRatesService{
final String fid;
DatabaseRatesService({this.fid});
DocumentReference get(){
return Firestore.instance.collection('rates').document(fid);}
RatesData _ratesDataFromSnapshot(DocumentSnapshot snapshot){
print(snapshot.data); // I put this here for testing purposes / it returns correct data here
return RatesData(
firstHourWeekday: snapshot.data['FirstHourWeekday'],
firstHourWeekend: snapshot.data['FirstHourWeekend'],
fid: fid,
ratesWeekday: snapshot.data['RatesWeekday'],
ratesWeekend: snapshot.data['RatesWeekend'],
);
}
Stream<RatesData> get ratesData {
DocumentReference ratesDocument = get();
return ratesDocument.snapshots()
.map(
_ratesDataFromSnapshot
);
}
}
更新:它没有返回null。我尝试获取错误消息。消息显示“'类型'列表'不是类型'列表'的子类型”。
答案 0 :(得分:0)
存在格式问题,if语句前后缺少{}括号 尝试以下
if(!snapshot.hasData)//在这种情况下不会下车并继续加载。 {return LoadingLogin();}
答案 1 :(得分:0)
根据您的comment,您需要编辑方法 _ratesDataFromSnapshot 来处理非原始数据类型以及空值(如果返回的数据不包含该字段,试图阅读)
snapshot.data['field'].cast<String>().toList() ?? List() // handle the null case as well with empty list