Flutter , type ‘Null’ is not a subtype of type ‘String’

Posted by

Error:

type 'Null' is not a subtype of type 'String'The relevant error-causing widget was:

Solution:

The reason for the error type ‘Null’ is not a subtype of type ‘String’ is that you are attempting to use a value that might be null as if it were a String no matter what. It appears that in your case, the _dropdownValue variable is the problem.

Although you initialised _dropdownValue to be ‘Select Council,’ you are using the?? operator when setting its value in initState. This means that if widget.registrationData[‘regcouncil’] is null, ‘Select Council’ will be used instead. You must make sure the value you assign to _dropdownValue is not null, though, as it is still a String and not String? (nullable String).

You can update _dropdownValue’s initialisation to be nullable (String?) and modify the initialisation in

late String? _dropdownValue;

Then, in initState, set _dropdownValue based on whether widget.registrationData['regcouncil'] is null or not:

_dropdownValue = widget.registrationData['regcouncil'] as String? ?? 'Select Council';

This ensures that _dropdownValue is always of type String?, which can handle null values, and defaults to 'Select Council' if the value is null.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x