The getter " type" was called on null. error in flutter

1143 views flutter dropdown
6

I have a function like this,

  changingTheSubjectOptions() {
    if (registerType.type == "Teacher") {
      subjectsOptionShow = true;
    } else {
      subjectsOptionShow = false;
    }
    return subjectsOptionShow;
  }

Here the value of registerType.type is changing when the user selects the dropdown option here,

  RegisterType registerType;
  List<RegisterType> registrationList = <RegisterType>[
    RegisterType(type: "Student"),
    RegisterType(type: "Teacher"),
  ];

DropdownButton<RegisterType>(
                    value: registerType,
                    icon: Icon(Icons.arrow_downward),
                    iconSize: 24,
                    elevation: 16,
                    hint: Container(
                      // width: 200,
                      child: Text("You are ",
                          style: TextStyle(
                              color: Colors.grey,
                              fontWeight: FontWeight.w600,
                              fontSize: 15)),
                    ),
                    underline: Container(
                      color: Colors.white,
                    ),
                    style: TextStyle(color: Colors.black),
                    onChanged: (RegisterType newValue) {
                      setState(() {
                        registerType = newValue;
                      });
                    },
                    items: registrationList
                        .map<DropdownMenuItem<RegisterType>>(
                            (RegisterType value) {
                      return DropdownMenuItem<RegisterType>(
                        value: value,
                        child: Text(value.type),
                      );
                    }).toList(),
                  ),

Now when I call the function like this,

Visibility(
                visible: changingTheSubjectOptions(),
                

it gives an error,
The getter 'type' was called on null.
Upto here I understand that registerType.type is null.
But also after I initialize the registerType.type in initState, it gives the same error.

  void initState() {
    registerType.type = "Student";
    super.initState();
  }

How can I initialize it?

answered question

1 Answer

3

registerType is null, not registerType.type. Maybe initialize it to something or use it with null checks.

posted this

Have an answer?

JD

Please login first before posting an answer.