[英]Get document id from firebase flutter
I trying to get the new created document id after data has been stored to firebase database, but get error 将数据存储到Firebase数据库后,我尝试获取新创建的文档ID,但出现错误
E/flutter (20333): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: NoSuchMethodError: The method 'listen' was called on null.
E/flutter (20333): Receiver: null
E/flutter (20333): Tried calling: listen(Closure: (String) => void)
send_data_bloc send_data_bloc
_repository
.addOrder(order)
.listen((documentId) => print(documentId));
repository 资料库
@override
Observable<String> addOrder(Order order) {
var a = endpoints.collectionEndpoint.add(order.toJson());
a.then((val) {
return Observable.fromFuture(val.documentID());
});
endpoints 终点
@override
get collectionEndpoint => _firestore
.collection(collectionName)
.document(this.id)
.collection(orderCollectionName);
Here 这里
a.then((val) {
return Observable.fromFuture(val.documentID());
});
you are returning the observable within the then function, i believe this is not the expected behavior. 您在then函数中返回了observable,我相信这不是预期的行为。
One thing you should do to improve your code quality and readability is to just user async/await. 要提高代码质量和可读性,您应该做的一件事就是仅使用户异步/等待。 The function on the repository can be rewrited like that:
存储库中的函数可以这样重写:
@override
Observable<String> addOrder(Order order) async {
var documentID = await endpoints.collectionEndpoint.add(order.toJson());
return Observable.fromFuture(val.documentID());
Try this. 尝试这个。 This should do the trick.
这应该可以解决问题。
Whats the reason why you are using Observables? 为什么使用Observables是什么原因? Is this a firebase thing?
这是火力基地吗?
You could adjust to: 您可以调整为:
final var documentId = await _repository.addOrder(order);
print(documentId)
Ideally you should return the future from the repository and await for the future on the bloc. 理想情况下,您应该从存储库中返回未来,并等待集团中的未来。 Let me try to give a full code snippet here.
让我尝试在此处提供完整的代码段。 It would be something like this:
就像这样:
send_data_bloc send_data_bloc
final documentId = await _repository
.addOrder(order);
print(documentId);
return documentId;
repository 资料库
@override
Future<String> addOrder(Order order) {
return endpoints.collectionEndpoint.add(order.toJson());
endpoints 终点
@override
get collectionEndpoint => _firestore
.collection(collectionName)
.document(this.id)
.collection(orderCollectionName);
i had the same problem here is a snippet of how i approached it 我有同样的问题,这是我如何处理的摘要
//notice im using add while referencing the document reference
final DocumentReference documentReference=await Firestore.instance.collection('jobs').add({
'jobid':"",
});
then get your id from documentReference 然后从documentReference获取您的ID
final String jobIdd=documentReference.documentID;
after getting the id now you can add your document to cloud firestore 现在获取ID后,您可以将文档添加到Cloud Firestore
Firestore.instance.collection('jobs').document(jobIdd).setData({
'category': category,
'description': description,
'datePosted': formattedDate,
'postedby': userid,
'jobid':jobIdd,
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.