将包含对象的类上传到Flutter中的Firestore

时间:2018-10-20 18:03:26

标签: firebase flutter google-cloud-firestore

我想上传一个包含班级用户列表的聊天室班级

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。 每当我收到一条错误消息,指出:

  

无效的参数:“用户”的实例

如果有人可以帮助您

0 个答案:

没有答案