首页 > 解决方案 > Flutter/Dart/Firestore:如何将地图列表添加到 Firebase?

问题描述

对不起,如果这是一个重复的问题。我尝试过四处搜索,但无法找到解决此问题的方法。

我有一个飞镖地图列表。例如:

List<Map<String, String>> questionsAndAnswers = [{'questions':'How many?', 'answer':'five'},
{'question':'How much?', 'answer':'five dollars'}];

我想将此地图列表存储到 Firestore 中,并且我已经看到您可以手动创建地图数组,但我想以编程方式执行此操作。

在此处输入图像描述

有谁知道如何做到这一点?我已经尝试过_firestore.collection('Quiz').add(questionsAndAnswers);,但这还没有成功,我收到以下错误:The argument type 'List<Map<String, String>>' can't be assigned to the parameter type 'Map<String, dynamic>'.

预先感谢您的所有帮助。

标签: firebaseflutterdartgoogle-cloud-firestore

解决方案


假设这是您的地图列表:

List<Map<String, String>> myData = [
  {'questions': 'How many?', 'answer': 'five'},
  {'question': 'How much?', 'answer': 'five dollars'},
];

将其添加到

  • 收藏

    FirebaseFirestore.instance 
        .collection('collection')
        .add({'some_key': myData});
    
  • 文件

    FirebaseFirestore.instance 
        .doc('collection/document')
        .set({'some_key': myData});
    

推荐阅读