Firebase 尝试使用Flatter从firestore提取活动值时出错
我试图从live firestore数据库中提取值,但代码中几乎没有错误。我使用streambuilder来提取数据,因为每当数据库发生更改时,数据都需要更新Firebase 尝试使用Flatter从firestore提取活动值时出错,firebase,flutter,google-cloud-firestore,Firebase,Flutter,Google Cloud Firestore,我试图从live firestore数据库中提取值,但代码中几乎没有错误。我使用streambuilder来提取数据,因为每当数据库发生更改时,数据都需要更新 class ProfileWidget extends StatelessWidget { final String userId; ProfileWidget(this.userId); @override Widget build(BuildContext context){ return StreamB
class ProfileWidget extends StatelessWidget {
final String userId;
ProfileWidget(this.userId);
@override
Widget build(BuildContext context){
return StreamBuilder<DocumentSnapshot>(
stream:Firestore.instance.collection('users')
.document(userId)
.snapshots(),
builder:(context, snapshot){
if(snapshot.hasData){
User user = User.fromSnapshot(snapshot.data);
return Row(
children: <Widget>[
Text(snapshot.data.documents[0]['sold'].toString())}
else
{return CircularProgressIndicator();
}
});
}
}
class User {
final int name;
final DocumentReference reference;
User.fromMap(Map<String, dynamic> map, {this.reference})
: name = map['name'];
User.fromSnapshot(DocumentSnapshot snapshot)
: this.fromMap(snapshot.data, reference: snapshot.reference);
}
class ProfileWidget扩展了无状态widget{
最终字符串用户标识;
ProfileWidget(this.userId);
@凌驾
小部件构建(构建上下文){
返回流生成器(
流:Firestore.instance.collection('用户')
.document(userId)
.snapshots(),
生成器:(上下文,快照){
if(snapshot.hasData){
User User=User.fromSnapshot(snapshot.data);
返回行(
儿童:[
文本(snapshot.data.documents[0]['sall'].toString())}
其他的
{返回循环ProgressIndicator();
}
});
}
}
类用户{
最终整数名;
最终文件参考;
User.fromMap(映射映射,{this.reference})
:name=map['name'];
User.fromSnapshot(DocumentSnapshot快照)
:this.fromMap(snapshot.data,reference:snapshot.reference);
}
以下是我遇到的两个错误:
对于第一个错误,假设您的每个用户都有
文档
属性,您应该这样访问它
snapshot.data['documents'][0]['sall'].toString()
第二个错误非常简单。您定义了用户,但从未在StreamBuilder的builder函数中使用过它