Null check operator used on a null value error on login form

51 Views Asked by At

════════ Exception caught by gesture ═══════════════════════════════════════════ Null check operator used on a null value ════════════════════════════════════════════════════════════════════════════════

class AuthForm extends StatefulWidget {
  final void Function(String email, String password, String username,
      File image, bool isLogin, BuildContext context) sumbitFn;
  final bool _isLoading;
  const AuthForm(this.sumbitFn, this._isLoading, {Key? key}) : super(key: key);

  @override
  State<AuthForm> createState() => _AuthFormState();
}

class _AuthFormState extends State<AuthForm> {
  bool binen = true;
  final _formkey = GlobalKey<FormState>();
  bool _isLogin = true;
  String _email = '';
  String _password = '';
  String _username = '';
  File? _userImageFile;

  void _pickedImage(File pickedImage) {
    _userImageFile = pickedImage;
  }

  void _submit() {
    final isValid = _formkey.currentState!.validate();
    FocusScope.of(context).unfocus();

    if (!_isLogin && _userImageFile == null) {
      ScaffoldMessenger.of(context).showSnackBar(SnackBar(
        content: const Text(
          'ڕەسمێکی خۆت دانە خۆ ناتخۆین',
          textAlign: TextAlign.right,
        ),
        backgroundColor: Theme.of(context).colorScheme.error,
      ));
      return;
    }

    if (isValid) {
      _formkey.currentState!.save();
      widget.sumbitFn(_email.trim(), _password.trim(), _username.trim(),
          _userImageFile!, _isLogin, context);
    }
  }
1

There are 1 best solutions below

0
Md. Yeasin Sheikh On

A common mistake is not providing the _formkey on Form widget like

Form(
 key:_formkey

Instead of using null-assert(!) it would be better to accept null or do a null check.

I am providing default value as false on null case

  final isValid = _formkey.currentState?.validate()?? false;