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

Time:03-31

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
}

CodePudding user response:

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

CodePudding user response:

Do this change and you will be able to receive it as you want:

class User {
  String id;
  Map<String, 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
}

More information on dart Maps: https://dart.dev/guides/language/language-tour#maps

  • Related