Getting null check operator used on a null value when popping page with Provider package

Viewed 258

I have a login button which executes this function:

static Future loginPress(BuildContext context, TextEditingController emailController, TextEditingController passwordController) async {
    Loader(context);
    try {
      await MyUser.signIn(emailController.text.trim(), passwordController.text.trim());
      Navigator.of(context).pop();
      Navigator.of(context).pushReplacementNamed(Routes.authPage);
        
    } on CustomException catch (e) {
      Navigator.of(context).pop();
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text(e.message.toString()))
      );
    }
    return null;
  }

and the Loader above executes this:

Future<dynamic> Loader(BuildContext context) {
  return showDialog(
    context: context, 
    builder: (context){
      return Center(child: CircularProgressIndicator(color: COLOR_PRIMARY),);
    }
  );
}

The first code block has MyUser.signIn function as follows:

static Future signIn(String email, String password) async {
    if(email.isEmpty || password.isEmpty) throw CustomException('Either email or password field is empty');
    try {
      await FirebaseAuth.instance.signInWithEmailAndPassword(email: email, password: password);
    } on FirebaseAuthException catch (e) {
      print(e.message);
      throw CustomException('Wrong password or email');
    }
  }

_dirty:false
_hadUnsatisfiedDependencies:false
_inDirtyList:false
_inheritedWidgets:null
_lifecycleState:_ElementLifecycle (_ElementLifecycle.defunct)
_owner:BuildOwner
_parent:null
_slot:null
_state:null
_widget:null
debugDoingBuild:false
debugIsDefunct:true
depth:121
dirty:false
hashCode:138
owner:BuildOwner
renderObject:null
runtimeType:Type (StatefulElement)
size:<Cannot get size of inactive element.
In order for an element to have a valid size, the element must be active, which means it is part of the tree.
Instead, this element is in the _ElementLifecycle.defunct state.
The size getter was called for the following element:
  StatefulElement#0008a(DEFUNCT)
#0      Element.size.<anonymous closure> (package:flutter/src/widgets/framework.dart:4029:9)
#1      Element.size (package:flutter/src/widgets/framework.dart:4062:6)
#2      StatefulElement.Eval ()
#3      AuthViewModel.loginPress (package:better_health/view_model/auth_view_model.dart:49:17)
<asynchronous suspension>>
slot:null
state:<Null check operator used on a null value
#0      StatefulElement.state (package:flutter/src/widgets/framework.dart:4877:44)
#1      StatefulElement.Eval ()
#2      AuthViewModel.loginPress (package:better_health/view_model/auth_view_model.dart:49:17)
<asynchronous suspension>>
widget:<Null check operator used on a null value

The first code block where I do Navigator.of(context).pop(), I get the error Null check operator used on a null value but this only happens sometimes. Other times, it works fine. How can I fix this?

1 Answers

This solved it.

static Future loginPress(BuildContext context, TextEditingController emailController, TextEditingController passwordController) async {
    late BuildContext dialogContext;
    showDialog(
      context: context, 
      builder: (context) {
        dialogContext = context;
        return Center(child: CircularProgressIndicator(color: COLOR_PRIMARY),);
      }
    );
    try {
      await MyUser.signIn(emailController.text.trim(), passwordController.text.trim());
      Navigator.of(dialogContext).pop();
      Navigator.of(dialogContext).pushReplacementNamed(Routes.authPage);
        
    } on CustomException catch (e) {
      Navigator.of(context).pop();
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text(e.message.toString()))
      );
    }
    return null;
  }

Idk why. Maybe someone can fill me in.

Related