我正在尝试在抽屉中给用户起名字。
我已经按照其他问题的建议使用连接状态,我已经使用它,但是结果仍然为空。尝试调用抽屉时的结果是“糟糕,没有数据”
有人可以告诉我哪一部分不正确吗?非常感谢您的帮助。
DatabaseService:
class DatabaseService {
final String uid;
final String taskId;
DatabaseService({this.uid, this.taskId});
//collection reference
CollectionReference userCollection = Firestore.instance.collection('user');
//get user data stream
Stream<UserData> get userData {
return userCollection.document(uid).snapshots().map(_userDataFromSnapshot);
}
//user data from snapshot
UserData _userDataFromSnapshot(DocumentSnapshot snapshot) {
return UserData(fName: snapshot.data['first_name']);
}
} // DatabaseService
抽屉:
class DrawerWidget extends StatelessWidget {
final _auth = AuthService();
@override
Widget build(BuildContext context) {
final user = Provider.of<User>(context);
return StreamBuilder<UserData>(
stream: DatabaseService(uid: user.uid).userData,
builder: (context, snapshot) {
UserData userData = snapshot.data;
if (snapshot.hasData) {
switch (snapshot.connectionState) {
case ConnectionState.none:
case ConnectionState.waiting:
return Drawer(...);
default:
return Drawer(
child: Center(child: Text(userData.fName)), //Try to call the data here
);
}
} else {
return Drawer(
child: Center(child: Text('Oops, there is no data')),
);
}
});
}
}