这是我的流生成器的代码。
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
);
}
}
更新:它没有返回空值。我尝试收到错误消息。消息显示“'type 'List' 不是 type 'List' 的子类型”。