CodeCodeCodeSystem
CodeCodeCodeSystem

Reputation: 85

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. 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.

enter image description here

Does anyone know how this can be achieved? I've tried _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>'.

Upvotes: 5

Views: 4697

Answers (1)

CopsOnRoad
CopsOnRoad

Reputation: 267554

Say this is your list of 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});
    

Upvotes: 7

Related Questions