Загрузить класс с объектом в Firestore во Flutter

Я хочу загрузить класс чата, содержащий список пользователей класса

class ChatRoom {


List<User> members = List<User>();
  String chatRoomId;
  String key;
  String roomTitle;
  String lastMessage;
  FieldValue timestamp;
  String chatType;
  List<User> newMembers;

  ChatRoom(this.members, this.chatRoomId, this.roomTitle, this.lastMessage, this.timestamp, this.chatType);



  Map<String, dynamic> toJson() {
    return {
      "members": newMembers = members.map((User user) {
        return user.toJson().cast<User>();
      }).toList(),
      "chatRoomId": chatRoomId,
      "roomTitle": roomTitle,
      "timestamp": timestamp,
      "chatType": chatType,
      "lastMessage": lastMessage,
    };
  }

}

Вот класс пользователя:

class User {



String key;
  String firstName;
  String uid;
  String lastName;
  String email;
  String city;
  int age;
  int dateOfBirth;
  String gender;
  String profilePic;
  String hereFor;
  String seeking;
  String sexualOrientation;
  String aboutMe;
  String lookingPartner;
  String maritalStatus;
  String country;
  String username;
  int minage;
  int maxage;
  String fcmToken;
  bool invisible;
  double distance;
  String work;
  List<String> hobbies;
  bool hasKids;
  bool verified;


  User(this.firstName, this.uid, this.lastName, this.email, this.city,
      this.age, this.dateOfBirth, this.gender, this.profilePic, this.hereFor,
      this.seeking, this.sexualOrientation, this.aboutMe, this.lookingPartner,
      this.maritalStatus, this.country, this.username, this.minage, this.maxage,
      this.fcmToken, this.invisible, this.distance, this.work, this.hasKids, this.hobbies, this.verified);

  User.fromSnapshot(DocumentSnapshot snapshot) :
      key = snapshot.documentID,
      firstName = snapshot.data['firstName'],
      lastName = snapshot.data['lastName'],
      uid = snapshot.data['uid'],
      email = snapshot.data['email'],
      city = snapshot.data['city'],
      age = snapshot.data['age'],
      dateOfBirth = snapshot.data['dateOfBirth'],
      gender = snapshot.data['gender'],
      profilePic = snapshot.data['profilePic'],
      hereFor = snapshot.data['hereFor'],
      seeking = snapshot.data['seeking'],
      sexualOrientation = snapshot.data['sexualOrientation'],
      aboutMe = snapshot.data['aboutMe'],
      lookingPartner = snapshot.data['lookingPartner'],
      maritalStatus = snapshot.data['maritalStatus'],
      country = snapshot.data['country'],
      username = snapshot.data['username'],
        work = snapshot.data['work'] ?? 'N/A',
        hasKids = snapshot.data['hasKids'] ?? false,
        verified = snapshot.data['verified'] ?? false,
        hobbies = List<String>.from(snapshot.data['hobbies']),
      minage = snapshot.data['minage'],
      maxage = snapshot.data['maxage'],
      fcmToken = snapshot.data['fcmToken'],
        distance = snapshot.data['distance'],
      invisible = snapshot.data['invisible'];

  toJson() {
    return {
      "uid": uid,
      "firstName": firstName,
      "lastName": lastName,
      "age": age,
      "city": city,
      "dateOfBirth": dateOfBirth,
      "gender": gender,
      "profilePic": profilePic,
      "email": email,
      "hereFor": hereFor,
      "seeking": seeking,
      "sexualOrientation": sexualOrientation,
      "aboutMe": aboutMe,
      "lookingPartner": lookingPartner,
      "maritalStatus": maritalStatus,
      "country": country,
      "username": username,
      "maxage": maxage,
      "hobbies": hobbies,
      "verified": verified,
      "hasKids": hasKids,
      "work": work,
      "minage": minage,
      "distance": distance,
      "fcmToken": fcmToken,
      "invisible": invisible
    };
  }

}

Проблема, которую я получаю, заключается в том, что нет способа преобразовать каждого пользователя toJson, когда он находится в списке, поэтому он будет загружен в firestore в виде массива. Каждый раз, когда я получаю сообщение об ошибке:

Invalid argument: Instance of 'User'

Если бы кто-то мог помочь

Вы в этом разобрались?

dazza5000 20.11.2018 03:05

@ dazza5000 еще нет

Rlee128 21.11.2018 02:02

Кто-нибудь из вас, @ dazza5000, уже понял это?

M. A. 24.06.2021 12:37

@ M.A. попробуй это? stackoverflow.com/questions/50808513/…

dazza5000 24.06.2021 20:15
Интеграция Angular - Firebase Analytics
Интеграция Angular - Firebase Analytics
Узнайте, как настроить Firebase Analytics и отслеживать поведение пользователей в вашем приложении Angular.
0
4
258
0

Другие вопросы по теме