我将当前用户的电子邮件添加到他或她创建的每个文档中。
void _addData(){
Firestore.instance.runTransaction((Transaction transssaction) async{
CollectionReference reference = Firestore.instance.collection('pets');
await reference.add({
"owner's email" : owner,
"name" : name,
"especie" : especie,
"raza" : raza,
"sexo" : sexo,
"adquisicion" : _dateText
});
});
Navigator.pop(context);
}
在这里,我得到当前用户的电子邮件并将其保存在变量中,这样,每当他按下按钮时,电子邮件便会与他或她试图添加的宠物的数据一起保存
Padding(
padding: const EdgeInsets.only(top: 100.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
IconButton(
icon: Icon(Icons.check, size: 40.0),
onPressed: () async{
final currentUser = await FirebaseAuth.instance.currentUser();
owner = currentUser.email.toString();
_addData();
},
),
IconButton(
icon: Icon(Icons.close, size: 40.0),
onPressed: (){
Navigator.pop(context);
},
)
这是我在firebase中宠物收藏的规则
match/pets/{document=**}{
allow create, read, write: if request.auth.uid != null;
}
这是我用来从该集合中获取数据的查询
Padding(
padding: const EdgeInsets.only(top: 20.0),
child: StreamBuilder(
stream: Firestore.instance.collection('pets')
.where("owner's email", isEqualTo: userEmail).snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot){
if(!snapshot.hasData)
return new Container(child: Center(child: CircularProgressIndicator(),));
return PetList(document: snapshot.data.documents,);
}
),
),
总体上我遇到的问题是,它会获取pets集合中的所有文档