我正在使用Flutter和Firestore开发社交媒体应用程序。我所做的是,
class UserAttributes {
final String firstName;
final String secondName;
final String mobileNumber;
final String city;
final String country;
final int playerPoints;
final String playerType;
final String gender;
UserAttributes(
{this.firstName,
this.secondName,
this.mobileNumber,
this.city,
this.country,
this.playerPoints,
this.playerType,
this.gender});
}
2。从Firestore快照获取UserAttributes
List<UserAttributes> _userAttributesFromSnapShot(QuerySnapshot snapshot) {
return snapshot.documents.map((doc) {
return UserAttributes(
firstName: doc.data['First Name'] ?? '',
secondName: doc.data['Last Name'] ?? '',
mobileNumber: doc.data['Player Number'] ?? '',
city: doc.data['Player City'] ?? '',
country: doc.data['Player Country'],
playerPoints: doc.data['Player Points'] ?? 0,
playerType: doc.data['Player Type'] ?? '',
gender: doc.data['Gender'] ?? '',
);
}).toList();
}
Stream<List<UserAttributes>> get userdata {
return userCollection.snapshots().map(_userAttributesFromSnapShot);
}
4。在所需页面中设置StreamProvider(用星号标记)
class MapPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
**return StreamProvider<List<UserAttributes>>.value(
value: DatabaseService().userdata,**
child: Scaffold(
backgroundColor: Color(0xff09031D),
appBar: PreferredSize(
preferredSize: Size.fromHeight(30.0),
child: header()),
drawer: myDrawer(),
body: Container(child: MyUserData()),
),
);
}
5。从UserAttributes列表中打印值
class MyUserData extends StatelessWidget {
@override
Widget build(BuildContext context) {
final userData = Provider.of<List<UserAttributes>>(context);
userData.forEach((doc) {
print(doc.firstName);
print(doc.secondName);
print(doc.city);
print(doc.country);
print(doc.gender);
print(doc.playerPoints);
print(doc.playerType);
});
return Container();
}
}
我的问题是在打印UserAttributes时,我从数据库中获取所有集合。我只想登录用户的UserAttributes。我该如何实现?