Dart 如何在my friends集合中存储某人的用户ID,以及如何使用firestore with Flatter检索该用户的数据?
我想在我的朋友集合中存储我的朋友userid 我不知道如何在其他用户集合中存储某人的详细信息 这是我获取当前用户的代码Dart 如何在my friends集合中存储某人的用户ID,以及如何使用firestore with Flatter检索该用户的数据?,dart,flutter,google-cloud-firestore,Dart,Flutter,Google Cloud Firestore,我想在我的朋友集合中存储我的朋友userid 我不知道如何在其他用户集合中存储某人的详细信息 这是我获取当前用户的代码 Future<void> _getUserDoc() async { final FirebaseAuth _auth = FirebaseAuth.instance; final Firestore _firestore = Firestore.instance; FirebaseUser user = await _auth.cur
Future<void> _getUserDoc() async {
final FirebaseAuth _auth = FirebaseAuth.instance;
final Firestore _firestore = Firestore.instance;
FirebaseUser user = await _auth.currentUser();
setState(() {
userRef = _firestore.collection('users').document(user.uid);
});
}
这个问题有两部分-
1如何在user1 FRIENDS集合中存储user2id和user3id。
2如何检索user2数据字段并在小部件中显示
这是我的小部件代码,我想在其中显示user2的名称
Padding(
padding:
const EdgeInsets.only(left: 20.0, right: 20.0, top: 10.0),
child: Text(
'20',
style: TextStyle(
fontFamily: 'Montserrat',
fontWeight: FontWeight.bold,
fontSize: 17.0),
),
),
我想显示firestore的user2 age,而不是文本小部件中的20
提前感谢。我认为存储其他用户DocumentReference的最简单方法是,如果将其他用户DocumentReference存储在当前用户中,则可以使用FutureBuilder构建它们 在红色框内:friends是其他用户文档引用的数组 例如,您有一个用户模型
class UserModel {
final int age;
final String name;
final List<DocumentReference> friends;
UserModel(this.age, this.name, this.friends);
// to get from Firestore
UserModel.fromSnapshot(DocumentSnapshot snapshot):
age = snapshot['age'],
name = snapshot['name'],
friends = List.from(snapshot['friends']);
}
我希望这能解决您的问题。无法存储用户2您能提供更多信息吗?将代码张贴在您试图存储user2id的位置?
class UserModel {
final int age;
final String name;
final List<DocumentReference> friends;
UserModel(this.age, this.name, this.friends);
// to get from Firestore
UserModel.fromSnapshot(DocumentSnapshot snapshot):
age = snapshot['age'],
name = snapshot['name'],
friends = List.from(snapshot['friends']);
}
FutureBuilder(
future: currentUser.friends.first.get(), // this will take the first friend of current user
// or you can query the list here with where
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
return Center(child: Text('Connection State none'));
case ConnectionState.active:
case ConnectionState.waiting:
return Center(child: CircularProgressIndicator());
case ConnectionState.done:
if (snapshot.hasError)
return Center(child: Text('Error: ${snapshot.error}'));
return Padding(
padding:
const EdgeInsets.only(left: 20.0, right: 20.0, top: 10.0),
child: Text(
snapshot.data['age'],
style: TextStyle(
fontFamily: 'Montserrat',
fontWeight: FontWeight.bold,
fontSize: 17.0),
),
);
}
});