I want to add user uid to any document added.
I managed to get uid with the following code:
Future getCurrentUser() async {
final FirebaseUser user = await _auth.currentUser();
final uid = user.uid;
print(uid);
return uid.toString();
}
and I want to add it here :
void addData(){
databaseReference.collection(("ads"))
.add({
'category': '$category',
'location': '$location',
'subject': '$adName',
'userId': '$userid',
});
}
and when I try to define the variable userid in class I get the following error:
error: Expected a class member. (expected_class_member at [hpxksa]
lib/Screens/add_ad.dart:59) error: Only static members can be accessed in initializers.
and if I declare it inside the widget it is recorded in database like this:
getCurrentUser()
is asynchronous, therefore it returns a instance of Future
, you have to do the following:
void addData()async{
String userid = await getCurrentUser();
databaseReference.collection("ads").add({
'category': '$category',
'location': '$location',
'subject': '$adName',
'userId': '$userid',
});
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加