我需要在for ...循环之前将数据添加到Map或HashMap,在for ...循环期间将数据添加到Map,然后在循环之后创建包含所有数据的文档.
在Java for Android中我用过:
Map<String, Object> createDoc = new HashMap<>();
createDoc.put("type", type);
createDoc.put("title", title);
for (int x = 0; x < sArray.size(); x++) {
createDoc.put("data " + x,sArray.get(x));
}
firebaseFirestoreDb.collection("WPS").add(createDoc);
Run Code Online (Sandbox Code Playgroud)
我的问题是,我如何创建文档并立即获取它的ID然后更新/合并其余的数据?或者有没有办法将数据添加到Dart中的Map?
我在Dart找到的唯一一件事是:
Map<String, Object> stuff = {'title': title, 'type': type};
Run Code Online (Sandbox Code Playgroud)
并在for ...循环中:
stuff = {'docRef $x': docId};
Run Code Online (Sandbox Code Playgroud)
并在for ...循环之后:
Firestore.instance.collection('workouts').add(stuff);
Run Code Online (Sandbox Code Playgroud)
这将创建一个仅包含for ...循环中最后一个条目的文档.
我还导入了dart:collection来使用HashMap,但它不会让我使用
Map<String, Object> newMap = new HashMap<>();
Run Code Online (Sandbox Code Playgroud)
我收到错误:"A value of type 'HashMap' can't be assigned to a variable of type 'Map<String, Object>'"
先感谢您!