Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 正在分析dart中的对象(不支持的操作:无法添加到固定长度列表)_Arrays_Json_Flutter_Dart_Google Cloud Firestore - Fatal编程技术网

Arrays 正在分析dart中的对象(不支持的操作:无法添加到固定长度列表)

Arrays 正在分析dart中的对象(不支持的操作:无法添加到固定长度列表),arrays,json,flutter,dart,google-cloud-firestore,Arrays,Json,Flutter,Dart,Google Cloud Firestore,我有一个用户对象,该对象在用户登录/注册时保存到cloud firestore数据库。 因此,当用户登录时,将从数据库中检索用户对象,并且在我尝试对列表“usersProject”执行“添加”操作之前,一切都正常: // Add the new project ID to the user's project list user.userProjectsIDs.add(projectID); 因此,我得到了异常未处理的异常:不支持的操作:无法添加到固定长度的列表中 我认为问题在于将用户从jso

我有一个用户对象,该对象在用户登录/注册时保存到cloud firestore数据库。 因此,当用户登录时,将从数据库中检索用户对象,并且在我尝试对列表“usersProject”执行“添加”操作之前,一切都正常:

// Add the new project ID to the user's project list
user.userProjectsIDs.add(projectID);
因此,我得到了异常
未处理的异常:不支持的操作:无法添加到固定长度的列表中
我认为问题在于将用户从json转换为对象时,因为当用户注册时,对象将转换为json并存储在数据库中,用户将在转换之前使用对象自动登录

void createUser(String email, String password, String username, String name, String birthDate) async {
try {
  // Check first if username is taken
  bool usernameIsTaken = await UserProfileCollection()
      .checkIfUsernameIsTaken(username.toLowerCase().trim());
  if (usernameIsTaken) throw FormatException("Username is taken");

  // Create the user in the Authentication first
  final firebaseUser = await _auth.createUserWithEmailAndPassword(
      email: email.trim(), password: password.trim());

  // Encrypting the password
  String hashedPassword = Password.hash(password.trim(), new PBKDF2());

  // Create new list of project for the user
  List<String> userProjects = new List<String>();

  // Create new list of friends for the user
  List<String> friends = new List<String>();

  // Creating user object and assigning the parameters
  User _user = new User(
    userID: firebaseUser.uid,
    userName: username.toLowerCase().trim(),
    email: email.trim(),
    password: hashedPassword,
    name: name,
    birthDate: birthDate.trim(),
    userAvatar: '',
    userProjectsIDs: userProjects,
    friendsIDs: friends,
  );

  // Create a new user in the fire store database
  await UserProfileCollection().createNewUser(_user);
  
  // Assigning the user controller to the 'user' object
    Get.find<UserController>().user = _user;
    Get.back();

} catch (e) {
  print(e.toString());
}}
在这里,只有当用户注销然后登录时才会发生异常,但当他第一次注册时,将不会发生异常

 // Add the new project ID to the user's project list
user.userProjectsIDs.add(projectID);
登录功能

void signIn(String email, String password) async {
try {
  // Signing in
  FirebaseUser firebaseUser = await _auth.signInWithEmailAndPassword(email: email.trim(), password: password.trim());

  // Getting user document form firebase
  DocumentSnapshot userDoc = await UserProfileCollection().getUser(firebaseUser.uid);
 

  // Converting the json data to user object and assign the user object to the controller
  Get.find<UserController>().user = User.fromJson(userDoc.data);
  print(Get.find<UserController>().user.userName);

} catch (e) {
  print(e.toString());
}}

JSON解码可能会返回固定长度的列表,然后使用该列表初始化
User
类中的
userProjectsIDs
。这将阻止您添加其他元素

fromJson
构造函数更改以下内容:

userProjectsIDs=json['userProjectsIDs'].cast();

userProjectsIDs=List.of(json['userProjectsIDs'].cast());
void signIn(String email, String password) async {
try {
  // Signing in
  FirebaseUser firebaseUser = await _auth.signInWithEmailAndPassword(email: email.trim(), password: password.trim());

  // Getting user document form firebase
  DocumentSnapshot userDoc = await UserProfileCollection().getUser(firebaseUser.uid);
 

  // Converting the json data to user object and assign the user object to the controller
  Get.find<UserController>().user = User.fromJson(userDoc.data);
  print(Get.find<UserController>().user.userName);

} catch (e) {
  print(e.toString());
}}
class User {
  String userID;
  String userName;
  String email;
  String password;
  String name;
  String birthDate;
  String userAvatar;
  List<String> userProjectsIDs;
  List<String> friendsIDs;

  User(
      {this.userID,
      this.userName,
      this.email,
      this.password,
      this.name,
      this.birthDate,
      this.userAvatar,
      this.userProjectsIDs,
      this.friendsIDs});

  User.fromJson(Map<String, dynamic> json) {
    userID = json['userID'];
    userName = json['userName'];
    email = json['email'];
    password = json['password'];
    name = json['name'];
    birthDate = json['birthDate'];
    userAvatar = json['UserAvatar'];
    userProjectsIDs = json['userProjectsIDs'].cast<String>();
    friendsIDs = json['friendsIDs'].cast<String>();
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['userID'] = this.userID;
    data['userName'] = this.userName;
    data['email'] = this.email;
    data['password'] = this.password;
    data['name'] = this.name;
    data['birthDate'] = this.birthDate;
    data['UserAvatar'] = this.userAvatar;
    data['userProjectsIDs'] = this.userProjectsIDs;
    data['friendsIDs'] = this.friendsIDs;
    return data;
  }
}