Flutter Firebase - How to Add User uid to Document

Alex Ali

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:

enter image description here

Peter Haddad

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]

編集
0

コメントを追加

0

関連記事

分類Dev

How to create a collection inside a uid document in Firestore using Flutter?

分類Dev

Firebase auth, is the user uid security sensitive?

分類Dev

How does Firebase generate the Uid?

分類Dev

How to add map inside document in firestore using flutter?

分類Dev

Flutter/Firebase-How to perform delete document functionality when document ID is not known?

分類Dev

Firebase:User.uidを取得する方法は?

分類Dev

how to prevent access to other user with same user id (UID)

分類Dev

How do I fix this section of code to allow the user to get their favorites from Firebase Realtime db? even with uid working, user can't view favorites

分類Dev

How to get UID from an Angular fire store Document without snapshot

分類Dev

Check if document exists, if not create and add data Firebase

分類Dev

Firebase + Flutter : get platform user used to sign in

分類Dev

How can I display on a widget the current user data from Firebase to Flutter

分類Dev

How can I find my User ID (UID) from terminal?

分類Dev

Write and Read nested Map to Firebase Document from Flutter

分類Dev

I get my Firebase user Uid but its dataSnapshot keys value return null

分類Dev

will I hit Firebase limit if I update a document in subcollection of user simultaneously?

分類Dev

How to query a user for an array for a specific document?

分類Dev

How to retrieve an image from Firebase Storage, with Firebase UID as a part of the image URL?

分類Dev

how to add and delete a sub document in MongoDB shell?

分類Dev

Check if the Firebase user is authenticated for the first time using google in Flutter

分類Dev

User created with different UID and GID

分類Dev

How to await the upload of file to Firebase Storage in Flutter?

分類Dev

How to delete a Firebase Storage file with flutter?

分類Dev

How to upload pdf to firebase storage with flutter?

分類Dev

How to user string interpolation in Flutter to call a function

分類Dev

.document(uid)は.whereEqual( "uid"、uid)と同じですか?

分類Dev

How to listen for document changes in Cloud Firestore using Flutter?

分類Dev

How to query using unique document id in Firestore from flutter?

分類Dev

how to set custom object with document id to a collection in firestore in flutter?

Related 関連記事

  1. 1

    How to create a collection inside a uid document in Firestore using Flutter?

  2. 2

    Firebase auth, is the user uid security sensitive?

  3. 3

    How does Firebase generate the Uid?

  4. 4

    How to add map inside document in firestore using flutter?

  5. 5

    Flutter/Firebase-How to perform delete document functionality when document ID is not known?

  6. 6

    Firebase:User.uidを取得する方法は?

  7. 7

    how to prevent access to other user with same user id (UID)

  8. 8

    How do I fix this section of code to allow the user to get their favorites from Firebase Realtime db? even with uid working, user can't view favorites

  9. 9

    How to get UID from an Angular fire store Document without snapshot

  10. 10

    Check if document exists, if not create and add data Firebase

  11. 11

    Firebase + Flutter : get platform user used to sign in

  12. 12

    How can I display on a widget the current user data from Firebase to Flutter

  13. 13

    How can I find my User ID (UID) from terminal?

  14. 14

    Write and Read nested Map to Firebase Document from Flutter

  15. 15

    I get my Firebase user Uid but its dataSnapshot keys value return null

  16. 16

    will I hit Firebase limit if I update a document in subcollection of user simultaneously?

  17. 17

    How to query a user for an array for a specific document?

  18. 18

    How to retrieve an image from Firebase Storage, with Firebase UID as a part of the image URL?

  19. 19

    how to add and delete a sub document in MongoDB shell?

  20. 20

    Check if the Firebase user is authenticated for the first time using google in Flutter

  21. 21

    User created with different UID and GID

  22. 22

    How to await the upload of file to Firebase Storage in Flutter?

  23. 23

    How to delete a Firebase Storage file with flutter?

  24. 24

    How to upload pdf to firebase storage with flutter?

  25. 25

    How to user string interpolation in Flutter to call a function

  26. 26

    .document(uid)は.whereEqual( "uid"、uid)と同じですか?

  27. 27

    How to listen for document changes in Cloud Firestore using Flutter?

  28. 28

    How to query using unique document id in Firestore from flutter?

  29. 29

    how to set custom object with document id to a collection in firestore in flutter?

ホットタグ

アーカイブ