Home > Blockchain >  Getting the error "Null check operator used on a null value"
Getting the error "Null check operator used on a null value"

Time:04-24

Whenever I click on any textfield of the form, I get the following error in the screen:

Null check operator used on a null value See also https://flutter.dev/docs

Also, the console is showing the following error: The following ArgumentError was thrown resolving an image codec: Invalid argument(s): No host specified in URI file:///

Here is the code of the login form. I can't figure out why this error is showing up.

// .....
child: Form(
                    key: _formKey,
                    autovalidateMode: AutovalidateMode.disabled,
                    child: ListView.builder(
                      itemCount: 1,
                      shrinkWrap: false,
                      itemBuilder: (BuildContext context, int index) {
                        return Column(
                          children: [
                            SizedBox(height: 30),
                            HeroImage(),
                            SizedBox(height: 20),
                            Container(
                              child: Padding(
                                padding: const EdgeInsets.only(left: 20, right: 20),
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.stretch,
                                  children: [
                                    AppLableWidget(
                                      title: Languages.of(context)!.labelEmail,
                                    ),
                                    CardTextFieldWidget(
                                      focus: (v) {
                                        FocusScope.of(context).nextFocus();
                                      },
                                      textInputAction: TextInputAction.next,
                                      hintText: Languages.of(context)!.labelEnterYourEmailID,
                                      textInputType: TextInputType.emailAddress,
                                      textEditingController: _textEmail,
                                      validator: kvalidateEmail,
                                    ),
                                    Row(
                                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                      children: [
                                        AppLableWidget(
                                          title: Languages.of(context)!.labelPassword,
                                        ),
                                      ],
                                    ),
                                    CardPasswordTextFieldWidget(
                                        textEditingController: _textPassword,
                                        validator: kvalidatePassword,
                                        hintText: Languages.of(context)!.labelEnterYourPassword,
                                        isPasswordVisible: _passwordVisible),
                                    Row(
                                      mainAxisAlignment: MainAxisAlignment.end,
                                      crossAxisAlignment: CrossAxisAlignment.end,
                                      children: [
                                        /* Row(
                                          children: [
                                            Padding(
                                              padding: const EdgeInsets.all(10.0),
                                              child: ClipRRect(
                                                clipBehavior: Clip.hardEdge,
                                                borderRadius: BorderRadius.all(Radius.circular(5)),
                                                child: SizedBox(
                                                  width: 40.0,
                                                  height: ScreenUtil().setHeight(40),
                                                  child: Card(
                                                    shape: RoundedRectangleBorder(
                                                      borderRadius: BorderRadius.circular(10.0),
                                                    ),
                                                    child: Container(
                                                      child: Theme(
                                                        data: ThemeData(
                                                          unselectedWidgetColor: Colors.transparent,
                                                        ),
                                                        child: Checkbox(
                                                          value: isRememberMe,
                                                          onChanged: (state) =>
                                                              setState(() => isRememberMe = !isRememberMe),
                                                          activeColor: Colors.transparent,
                                                          checkColor: Color(Constants.colorTheme),
                                                          materialTapTargetSize: MaterialTapTargetSize.padded,
                                                        ),
                                                      ),
                                                    ),
                                                  ),
                                                ),
                                              ),
                                            ),
                                            Text(
                                              Languages.of(context)!.labelRememberMe,
                                              style: TextStyle(fontSize: 14.0, fontFamily: Constants.appFont),
                                            ),
                                          ],
                                        ),*/
                                        Container(
                                          padding: const EdgeInsets.all(10.0),
                                          alignment: Alignment.centerRight,
                                          child: GestureDetector(
                                            onTap: () {
                                              Navigator.of(context).push(Transitions(
                                                  transitionType: TransitionType.fade,
                                                  curve: Curves.bounceInOut,
                                                  reverseCurve: Curves.fastLinearToSlowEaseIn,
                                                  widget: ChangePassword()));
                                            },
                                            child: Text(
                                              Languages.of(context)!.labelForgotPassword,
                                              style: TextStyle(fontFamily: Constants.appFontBold,fontSize: ScreenUtil().setSp(16),),
                                            ),
                                          ),
                                        ),
                                      ],
                                    ),
                                    Padding(
                                      padding: EdgeInsets.only(left: 20.0, right: 20,top: 10, bottom: 10),
                                      child: RoundedCornerAppButton(
                                        onPressed: () {
                                          if (
                                          _formKey.currentState!.validate()) {
                                            // if (SharedPreferenceUtil.getString(
                                            //         Constants
                                            //             .appPush_oneSingleToken)
                                            //     .isEmpty) {
                                            //   getOneSingleToken(SharedPreferenceUtil
                                            //       .getString(Constants
                                            //           .appSettingCustomerAppId));
                                            // } else {
                                            // }
                                            Constants.checkNetwork().whenComplete(() => callUserLogin());
                                          } else {
                                            setState(() {
                                              // validation error
                                              //_autoValidate = true;
                                            });
                                          }
                                        },
                                        btnLabel: Languages.of(context)!.labelLogin,
                                      ),
                                    ),
                                    SizedBox(
                                      height: 10.0,
                                    ),
                                    GestureDetector(
                                      onTap: () {
                                        Navigator.of(context).push(Transitions(
                                            transitionType: TransitionType.slideUp,
                                            curve: Curves.bounceInOut,
                                            reverseCurve: Curves.fastLinearToSlowEaseIn,
                                            widget: CreateNewAccount()));
                                      },
                                      child: Row(
                                        mainAxisAlignment: MainAxisAlignment.center,
                                        children: [
                                          Text(
                                            Languages.of(context)!.labelDonthaveAcc,
                                            style: TextStyle(fontFamily: Constants.appFont,fontSize: ScreenUtil().setSp(14),),
                                          ),
                                          Text(
                                            Languages.of(context)!.labelCreateNow,
                                            style: TextStyle(fontFamily: Constants.appFontBold,fontSize: ScreenUtil().setSp(16),),
                                          ),
                                        ],
                                      ),
                                    ),
                                    SizedBox(
                                      height: 20,
                                    ),
                                    InkWell(
                                      onTap: () {
                                        Navigator.of(context).push(Transitions(
                                            transitionType: TransitionType.slideUp,
                                            curve: Curves.bounceInOut,
                                            reverseCurve: Curves.fastLinearToSlowEaseIn,
                                            widget: DashboardScreen(0)));

                                      },
                                      child: Row(
                                        mainAxisAlignment: MainAxisAlignment.center,
                                        children: [
                                          Text(
                                            Languages.of(context)!.labelSkipNow,
                                            style: TextStyle(
                                              color: Colors.black,
                                              decoration: TextDecoration.underline,
                                              decorationColor: Colors.black,
                                              fontWeight: FontWeight.bold,
                                              decorationThickness: 5,
                                              fontSize: ScreenUtil().setSp(16),
                                              fontFamily: Constants.appFontBold,),
                                          ),
                                        ],
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            ),
                          ],
                        );
                      },
                    ),
                  ),
  //.....

  String kvalidatePassword(String? value) {
    Pattern pattern = r'^(?=.*?[a-z])(?=.*?[0-9]).{8,}$';
    if (value!.isEmpty) return 'Username is Required.';
    final RegExp nameExp = new RegExp(pattern as String);
    if (!nameExp.hasMatch(value))
      return 'Please enter only alphabetical characters.';
    return value;
  }
  
  String kvalidatePassword(String? value) {
    Pattern pattern = r'^(?=.*?[a-z])(?=.*?[0-9]).{8,}$';
    if (value!.isEmpty) return 'Username is Required.';
    final RegExp nameExp = new RegExp(pattern as String);
    if (!nameExp.hasMatch(value))
      return 'Please enter only alphabetical characters.';
    return value;
  }

Please help. New to Flutter and this is an old project I need to fix.

CodePudding user response:

 AppLableWidget(title: Languages.of(context)!.labelEmail),

Try This...

 AppLableWidget(title: Languages.of(context)?.labelEmail ?? ''),
String value = 'Test String';
String? value1;
 Text(value ?? ''), // output --> Test String 
 Text(value1 ?? ''), // output --> ''
// here manage if value are null then return ''(Blank Text) otherwise Text.

CodePudding user response:

This error occurs when you use a bang operator (!) on a nullable instance which wasn't initialized.

For example:

String? foo; // Nullable String

void main() {
  var len = foo!.length; // Runtime error: Null check operator used on a null value
}

Solution:

You need to find out where you're using the bang operator in your code. Once you are there, you can use any of the following solutions:

Use a local variable

var f = foo;
if (f != null) {
  var len = f.length; // Safe 
}

Use ?. and ??

var len = foo?.length ?? 0; // Provide a default value if foo was null.
  • Related