简体   繁体   中英

Flutter Firebase Firestore

i have one question, i have created 1 docs that have id using Uuid().v4(), i want to know how do i call that docs using that id because i want to update the data inside that docs on the next page. Below is my code to create the docs. Before this i only try to update the user data and it was quite easy because i can use firebaseauth to get the id and i am not sure how to get data from other docs.

_payment() async {
    if (balance >= price!) {
      try {
        final FirebaseAuth _auth = FirebaseAuth.instance;
        User? user = _auth.currentUser;
        final _uid = user!.uid;
        final transactionId = Uuid().v4();

        await FirebaseFirestore.instance
            .collection('transaction')
            .doc(transactionId)
            .set({
          'transactionId': transactionId,
          'clientId': _uid,
          'freelancerId': widget.uploadedBy,
          'jobTitle': jobTitle,
          'price': price,
          'status': status,
        });

        await FirebaseFirestore.instance
            .collection("users")
            .doc(_uid)
            .update({"balance": balance - price!});

      
      } catch (error) {}
    }
  }

Split your expression into 3 parts.

The first part for getting document reference

final document = await FirebaseFirestore.instance
  .collection('transaction')
  .doc('transactionId');

Second, fill your document with data

document.set({
 'transactionId': transactionId,
 'clientId': _uid,
 'freelancerId': widget.uploadedBy,
 'jobTitle': jobTitle,
 'price': price,
 'status': status,
});

The last one is retrieving the document id

final documentId = document.id;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM