How to create user model with nested map field for cloud firestore with flutter?

Issue

I want to create a nested field with the type of map. ‘usersName’ is the field that has the type of map, contain ‘firstName’ and ‘lastName’. Here is the image:
enter image description here
If you need a source code, tell me. Thank you

user_model.dart

class User {
  String id;
  String name;
  String email;
  String password;
  String phone;

  User({
    required this.id,
    required this.name,
    required this.email,
    required this.password,
    required this.phone,
  });

  //and i dont know how to make it work
}

Solution

You can create a UserName class with firstName and lastName variables and then on your User class you change your name from String to UserName.

class User {
 String id;
 UserName name;
 String email;
 String password;
 String phone;

 User({
   required this.id,
   required this.name,
   required this.email,
   required this.password,
   required this.phone,
 });
}

class UserName {
   String firstName;
   String lastName;
   UserName({
      required this.firstName,
      required this.lastName,
   });
}

Answered By – Henrique Zanferrari

Answer Checked By – Katrina (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *