Home > database >  How do I add a map to a map document field in flutter?
How do I add a map to a map document field in flutter?

Time:02-27

How do I add a map to a type map field in Firebase using flutter? enter image description here

enter image description here

Here is the code in the image


FirebaseFirestore.instance.collection('userNames').doc(docID).set(
              {
                'children': {
                  'childUserName': username.text,
                  'childPassword': password.text,
                  'childFirstName': firstName.text,
                  'childLastName': lastName.text,
                  'username': username.text,
                  'parentUID': currentUID,
                  'parentFirstName': parentFirstName,
                  'parentLastName': parentLastName,
                  'points': startingAmount.text,
                  'timestamp': DateTime.now().millisecondsSinceEpoch,
                }
              },
              SetOptions(merge: true),
            )

CodePudding user response:

You can write a Children and Child data model, then write a toMap function using.

example:


class Children {
  final Child child;
  Children({
    required this.child,
  });

  Map<String, dynamic> toMap() {
    return {
      'child': child.toMap(),
    };
  }
}


class Child {
  final String hello;
  Child({
    required this.hello,
  });

  Map<String, dynamic> toMap() {
    return {
      'hello': hello,
    };
  }
}

  • Related