Mohammad Abd-Elmoniem
Mohammad Abd-Elmoniem

Reputation: 666

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

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),
            )

Upvotes: 1

Views: 895

Answers (1)

Daviswww
Daviswww

Reputation: 451

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,
    };
  }
}

Upvotes: 3

Related Questions