【问题标题】:Firestore / Flutter - How can I get document Id?Firestore / Flutter - 如何获取文档 ID?
【发布时间】:2019-05-05 10:43:44
【问题描述】:

这是我的代码

Future _save() async {

final StorageReference storageRef = FirebaseStorage.instance.ref().child('product/'+nameController.text+'.jpg');
final StorageUploadTask task = storageRef.putFile(_image);
StorageTaskSnapshot taskSnapshot = await task.onComplete;
String downloadUrl = await taskSnapshot.ref.getDownloadURL();
StorageMetadata created = await taskSnapshot.ref.getMetadata();

Firestore.instance.collection('product').document()
    .setData({
  'name': nameController.text,
  'price': int.tryParse(priceController.text),
  'description': descriptionController.text,
  'creator': widget.user.uid,
  'created': DateTime.fromMillisecondsSinceEpoch(created.creationTimeMillis, isUtc: true).toString(),
  'modified': DateTime.fromMillisecondsSinceEpoch(created.updatedTimeMillis, isUtc: true).toString(),
  'url': downloadUrl,
  'id': //I want to set document Id here //
});
}

我怎样才能得到这个随机生成的文档的 ID? 谢谢你的帮助

【问题讨论】:

    标签: firebase flutter google-cloud-firestore


    【解决方案1】:

    使用 value.Id 获取部分 documentId

     CollectionReference users = FirebaseFirestore.instance.collection('candidates');
          
          Future<void> registerUser() {
         // Call the user's CollectionReference to add a new user
             return users.add({
              'name': enteredTextName, // John Doe
              'email': enteredTextEmail, // Stokes and Sons
              'profile': dropdownValue ,//
              'date': selectedDate.toLocal().toString() ,//// 42
            })
                .then((value) =>(showDialogNew(value.id)))
                .catchError((error) => print("Failed to add user: $error"));
          }
    

    【讨论】:

      【解决方案2】:

      cloud_firestore 包的 v0.14.0 开始:

      已弃用:documentID 已弃用,取而代之的是 id。

      所以而不是

      ref.documentId
      

      使用

      ref.id
      

      检索随机生成的文档 id。

      【讨论】:

        【解决方案3】:

        您可以通过调用您的 ref.documentID 来获取 ID

        【讨论】:

          【解决方案4】:

          你就是这样的

          DocumentReference documentReference = Firestore.instance.collection('product').document();    
          documentReference.setData({
                  'name': nameController.text,
                  'price': int.tryParse(priceController.text),
                  'description': descriptionController.text,
                  'creator': widget.user.uid,
                  'created': DateTime.fromMillisecondsSinceEpoch(created.creationTimeMillis, isUtc: true).toString(),
                  'modified': DateTime.fromMillisecondsSinceEpoch(created.updatedTimeMillis, isUtc: true).toString(),
                  'url': downloadUrl,
                  'id': documentReference.documentID
                });
          

          文档编号

          documentReference.documentID
          

          【讨论】:

            【解决方案5】:

            collection 之后,您可以添加document 并接收DocumentReference

              final docRef = await Firestore.instance.collection('product').add({
                'name': nameController.text,
                'price': int.tryParse(priceController.text),
                'description': descriptionController.text,
                'creator': widget.user.uid,
                'created': DateTime.fromMillisecondsSinceEpoch(created.creationTimeMillis, isUtc: true).toString(),
                'modified': DateTime.fromMillisecondsSinceEpoch(created.updatedTimeMillis, isUtc: true).toString(),
                'url': downloadUrl,
              });
            

            现在可以获取文档ID了:

             docRef.documentID 
            

            【讨论】:

              猜你喜欢
              • 2021-07-20
              • 2020-12-05
              • 2022-07-22
              • 2020-03-09
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2021-01-28
              • 2021-12-16
              相关资源
              最近更新 更多