Search code examples
arraysjsonflutterdartgoogle-cloud-firestore

Parsing object in dart (Unsupported operation: Cannot add to a fixed-length list)


I have a user object which saved to the cloud firestore database when the user Sign In / Sign Up. So the user object is retrieved from the database when he signs in, and everything works until i try to do the 'add' operation on the list 'usersProject':

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

So i get the exception Unhandled Exception: Unsupported operation: Cannot add to a fixed-length list i believe the problem is when converting the user from json to object, because when the user is signing up the object is converted to json and stored in the database and the user will be automatically signed in using the object before converting.

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

When the user is signed off then he signs in and try to make operation on the user object, here comes the problem some of the properties (the List type) can't be used. This code creates project and add projectID to the user's list

  Future<void> createNewProject(String projectName, User user) async {

String projectID = Uuid().v1(); // Project ID, UuiD is package that generates random ID

// Add the creator of the project to the members list and assign him as admin
var member = Member(
  memberUID: user.userID,
  isAdmin: true,
);
List<Member> membersList = new List();
membersList.add(member);

// Save his ID in the membersUIDs list
List <String> membersIDs = new List();
membersIDs.add(user.userID);

// Create chat for the new project
var chat = Chat(chatID: projectID);

// Create the project object
var newProject = Project(
  projectID: projectID,
  projectName: projectName,
  image: '',
  joiningLink: '$projectID',
  isJoiningLinkEnabled: true,
  pinnedMessage: '',
  chat: chat,
  members: membersList,
  membersIDs: membersIDs,
);


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

try {
  // Convert the project object to be a JSON
  var jsonUser = user.toJson();

  // Send the user JSON data to the fire base
  await Firestore.instance
      .collection('userProfile')
      .document(user.userID)
      .setData(jsonUser);

  // Convert the project object to be a JSON
  var jsonProject = newProject.toJson();

  // Send the project JSON data to the fire base
  return await Firestore.instance
      .collection('projects')
      .document(projectID)
      .setData(jsonProject);
} catch (e) {
  print(e);
}}

Here where the exception happens only when the user signs off then signs in, but when he signed up for the first time there will be no exception.

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

The sign in function

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

I think the problem caused by User.fromJson why it makes the array from the firestore un-modifiable ?

The user class

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


Solution

  • Your JSON decoding is likely returning fixed-length lists, which you're then using to initialize userProjectsIDs in the User class. This prevent you from adding additional elements.

    Change the following from the fromJson constructor:

    userProjectsIDs = json['userProjectsIDs'].cast<String>();
    

    to

    userProjectsIDs = List.of(json['userProjectsIDs'].cast<String>());