iampawan / 30DaysOfFlutter

Learn Flutter in 30 Days
410 stars 324 forks source link

Error in `day8` code #43

Open Shivansh-yadav13 opened 1 year ago

Shivansh-yadav13 commented 1 year ago

https://github.com/iampawan/30DaysOfFlutter/blob/e7578cffb012838d1051c85b0766169821e918a0/lib/pages/login_page.dart#L65

This does not work anymore, it gives an error

The property 'isEmpty' can't be unconditionally accessed because the receiver can be 'null'. 

Similarly,

https://github.com/iampawan/30DaysOfFlutter/blob/e7578cffb012838d1051c85b0766169821e918a0/lib/pages/login_page.dart#L16

This also give error

The method 'validate' can't be unconditionally invoked because the receiver can be 'null'. 
lazysquid7 commented 1 year ago

Did you find any alternative how to do it?

lazysquid7 commented 1 year ago

value.isEmpty can be replaced with =>> value == null / value.isNull But how to resolve other issue?

lazysquid7 commented 1 year ago

Use below code to resolve the issue. If you are getting an error in the TextFormField validator, your condition should be : validator: (value) { if (value ==null || value.isEmpty) { return "Username cannot be empty"; }

Similar in both username and password.

And in the moveToHome() method validation condition should be: if(_formkey.currentState!.validate()){};

malik-vishu commented 1 year ago

`validator: (value) { if (value != null && value.isEmpty) { return "Username cannot be empty"; }

  return null;

}`

`moveToHome(BuildContext context) async { if (formKey.currentState != null && formKey.currentState!.validate()) { setState(() { change = true; });

  await Future.delayed(const Duration(seconds: 1));
  await Navigator.pushNamed(context, AllRoutes.homeRoute);
  setState(() {
    //rendering again on coming back
    change = false;
  });
}

}`