title: Flutter : This widget has been unmounted, so the State no longer has a context (and should be considered defunct) Error when using streambuilder
body: <pre><code>    class _MyAppState extends State&lt;MyApp&gt; {
    bool isFirstTime = true;
    bool? isRegistered;
    checkIfRegisteredOrNot() async {
      print(&quot;Checking if registered&quot;);
      if (isFirstTime &amp;&amp; mounted) {
        print(&quot;Insided mounted&quot;);
        final id = FirebaseAuth.instance.currentUser!.uid;
        DocumentSnapshot ds =
            await FirebaseFirestore.instance.collection(&quot;users&quot;).doc(id).get();
        setState(() {
          print(&quot;Setting state&quot;);
          isRegistered = ds.exists;
          isFirstTime = false;
          print(&quot;isRegistered: $isRegistered&quot;);
          print(&quot;isFirstTime: $isFirstTime&quot;);
        });
      }
    }
  
    // This widget is the root of your application.
    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        title: 'SehatMand',
        theme: ThemeData(
            scaffoldBackgroundColor: Color(0xFF222831),
            inputDecorationTheme: InputDecorationTheme(
              enabledBorder: OutlineInputBorder(
                  borderRadius: BorderRadius.circular(2),
                  borderSide: BorderSide(color: Colors.deepPurpleAccent)),
              border: OutlineInputBorder(
                borderRadius: BorderRadius.circular(2),
                borderSide: BorderSide(color: Colors.deepPurpleAccent, width: 5),
              ),
            ),
            colorScheme: ColorScheme.dark().copyWith(
              secondary: Colors.deepPurpleAccent,
            )),
        home: StreamBuilder(
          stream: FirebaseAuth.instance.authStateChanges(),
          builder: (BuildContext context, AsyncSnapshot&lt;dynamic&gt; snapshot) {
            if (snapshot.hasData) {
              print(&quot;Has data&quot;);
              checkIfRegisteredOrNot();
              if (isRegistered != null) {
                return isRegistered! ? const TestScreen() : const FormScreen();
              } else {
                print(&quot;isRegistered is null&quot;);
                return const Scaffold(
                    body: Center(child: CircularProgressIndicator()));
              }
            } else {
              print('LoginScreen');
              return const LoginScreen();
            }
          },
        ),
        routes: {
          MainScreen.routeName: (context) =&gt; MainScreen(),
          FormScreen.routeName: (context) =&gt; const FormScreen(),
        },
      );
    }
  }
  </code></pre>
  <p>Description of the error i am getting</p>
  <blockquote>
  <p>======== Exception caught by scheduler library ===================================================== The following assertion was thrown during a scheduler callback: This widget has been
  unmounted, so the State no longer has a context (and should be
  considered defunct).</p>
  </blockquote>
  <p>**&gt; Consider canceling any active work during &quot;dispose&quot; or using the</p>
  <blockquote>
  <p>&quot;mounted&quot; getter to determine if the State is still active. When the
  exception was thrown, this was the stack:
  #0      State.context. (package:flutter/src/widgets/framework.dart:942:9)
  #1      State.context (package:flutter/src/widgets/framework.dart:948:6)
  #2      _TransformerPageViewState._onGetSize (package:another_transformer_page_view/src/another_transformer_page_view.dart:450:24)
  #3      SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1144:15)
  #4      SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1089:9)
  #5      SchedulerBinding.scheduleWarmUpFrame. (package:flutter/src/scheduler/binding.dart:862:7) (elided 11 frames
  from class _RawReceivePortImpl, class _Timer, dart:async, and
  dart:async-patch)
  ** ====================================================================================================</p>
  </blockquote>