“NoSuchMethodError: The getter 'length' was called on null” while performing login

Please help:
NoSuchMethodError: The getter ‘length’ was called on null.

Hey @Gaurav_Sharma with only the error we can’t do much. Paste the part of the code that is giving you trouble.

Only looking at the error it looks like you a calling .length on a variable that has not jet been initialize.

If you are using provider or something like that, take into consideration that it may take some time for the variable to refresh and get the value. For that you can use ?. This will tell the app that that variable may not have any value. For example, is you are listening to a User provider to know when the user authenticated you may have something like this:

/// This will give you an error

/// But if you add ?, The app won't break

In any case, you should find where is the problem and wait until it is initialize, showing a loading or something while it is not.

Hope that helps. If not, post the code and I’ll get back to you. :wink:

1 Like

@Gaurav_SharmaI checked the stackoverflow attachment that you shared. I see that below is the only line where the getter length is called.

validator: (val) => val.length < 6 ? 'Invalid Password' : null,

I would suggest you change this to

validator: (val) => val.isNotEmpty && val.length >= 6 ? null : 'Invalid Password',

This should resolve your issue.

I added onSaved: (val) => _password = val, onSaved: (val) => _email = val, under each text form input field respectively. Now it returns error:
Unhandled Exception: type ‘List’ is not a subtype of type ‘String’

@sakina Please check the modified problem on stackoverflow