NoSuchMethodError. The method "add" was called on null even after initializing List

Viewed 104

I have created a TextFormField in flutter. I have a class named ParticipantsData with a few properties listed. I can access and store values in all of those properties by making an object of ParticipantsData class in another class named "RegistrationForm". However I am unable to store data in the properties that are of type List even after having initialized them.

I have tried:

 - List.filled() 
 - =[]
 - =[""]
 - List.generate()
 - List()
 - List<String>()
 - List<String>(length)

I have changed my code multiple times over and tried many methods but nothing seems to work. I don't post here much because I usually find solutions on stackoverflow but this time I couldn't find anything. Unable to post the whole code because it is too long. Below is the relevant code:

ParticipantsData class:

class ParticipantsData {
  List name = []; //members
  bool paymentstatus = false; //payment
  String email = ""; //email
  String address = ""; //address
  List contact = []; //contact
  String collegename = ""; //collegename
  String password = ""; //password
  String teamname = ""; //teamname
  var modules = List<String>(6); //modules

  ParticipantsData({
    this.name,
    this.email,
    this.contact,
    this.collegename,
    this.address,
    this.modules,
    this.password,
    this.paymentstatus,
    this.teamname,
  });
}

Below is the relevant code for Register class:

class _RegistrationForm extends State<RegistrationForm> {
  final ParticipantsData data = new ParticipantsData();

//This is the onSaved method of a TextFormField, which is in a loop.
(String value) {                                         //Tried this...
                  data.name[i + 1] = value;
                  print('${data.name[i + 1]}');
                }),

(String value) {                                             //And this too...
                  data.name.add(value); 
                  print('${data.name[i + 1]}');
                }),
1 Answers

The assigned values become null when initialized inside the constructor. If you don't include the fields in the constructor, they will not reset to null.

However, you may want to assign the values in the constructor and want some default value if the field is not provided while creating the instance. Here is how to do so:

class MyClass {

  // Don't initialize here
  List x;
  int y;
  
  MyClass({
      this.x,
      this.y = 10, // If y is not assigned, it will take a default value of 10
  }) {
    // Constructor body
    this.x = this.x ?? []; // If x is not assigned, it will take a value of []
  }
}

Notice that y can be provided with the default value directly as 10 is a constant value. You can only assign constant default values in the constructor parameter list. Since [ ] is not a constant expression or value, it can't be directly assigned as the default value hence, you need to define the constructor body assigning x = [ ] if x is null.

this.x = this.x ?? [];

You can initialize the others in a similar way.

Related