[英]Flutter/Dart/Firestore: How can I add a list of maps to firebase?
Sorry if this is a repeated question.对不起,如果这是一个重复的问题。 I have tried searching around but have been unable to find a solution to this problem.
我尝试过四处搜索,但无法找到解决此问题的方法。
I have a list of maps in dart.我在 dart 中有一个地图列表。 For example:
例如:
List<Map<String, String>> questionsAndAnswers = [{'questions':'How many?', 'answer':'five'},
{'question':'How much?', 'answer':'five dollars'}];
I would like to store this list of maps into firestore and I have seen that you can manually create an array of maps but I would like to do this programmatically.我想将此地图列表存储到 Firestore 中,并且我已经看到您可以手动创建地图数组,但我想以编程方式执行此操作。
Does anyone know how this can be achieved?有谁知道如何做到这一点? I've tried
_firestore.collection('Quiz').add(questionsAndAnswers);
我试过
_firestore.collection('Quiz').add(questionsAndAnswers);
but this hasn't been successful and I'm receiving the following error: The argument type 'List<Map<String, String>>' can't be assigned to the parameter type 'Map<String, dynamic>'.
但这并不成功,我收到以下错误:
The argument type 'List<Map<String, String>>' can't be assigned to the parameter type 'Map<String, dynamic>'.
Say this is your list of map:假设这是您的 map 列表:
List<Map<String, String>> myData = [
{'questions': 'How many?', 'answer': 'five'},
{'question': 'How much?', 'answer': 'five dollars'},
];
Adding it to a将其添加到
Collection :收藏:
FirebaseFirestore.instance.collection('collection').add({'some_key': myData});
Document :文件:
FirebaseFirestore.instance.doc('collection/document').set({'some_key': myData});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.