educative.io

Provider.of<LoggedUser> is null

I am trying to follow the course until StartApp Screen topic. I got this kind of error because of Provider.of is null.

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Error: Could not find the correct Provider<LoggedUser> above this SignUpForm Widget
E/flutter (21378): 
E/flutter (21378): This happens because you used a `BuildContext` that does not include the provider
E/flutter (21378): of your choice. There are a few common scenarios:
E/flutter (21378): 
E/flutter (21378): - You added a new provider in your `main.dart` and performed a hot-reload.
E/flutter (21378):   To fix, perform a hot-restart.
E/flutter (21378): 
E/flutter (21378): - The provider you are trying to read is in a different route.
E/flutter (21378): 
E/flutter (21378):   Providers are "scoped". So if you insert of provider inside a route, then
E/flutter (21378):   other routes will not be able to access that provider.
E/flutter (21378): 
E/flutter (21378): - You used a `BuildContext` that is an ancestor of the provider you are trying to read.
E/flutter (21378): 
E/flutter (21378):   Make sure that SignUpForm is under your MultiProvider/Provider<LoggedUser>.
E/flutter (21378):   This usually happens when you are creating a provider and trying to read it immediately.
E/flutter (21378): 
E/flutter (21378):   For example, instead of:
E/flutter (21378): 
E/flutter (21378):   ```
E/flutter (21378):   Widget build(BuildContext context) {
E/flutter (21378):     return Provider<Example>(
E/flutter (21378):       create: (_) => Example(),
E/flutter (21378):       // Will throw a ProviderNotFoundError, because `context` is associated
E/flutter (21378):       // to the widget that is the parent of `Provider<Example>`
E/flutter (21378):       child: Text(context.watch<Example>().toString()),
E/flutter (21378):     );
E/flutter (21378):   }
E/flutter (21378):   ```
E/flutter (21378): 
E/flutter (21378):   consider using `builder` like so:
E/flutter (21378): 
E/flutter (21378):   ```
E/flutter (21378):   Widget build(BuildContext context) {
E/flutter (21378):     return Provider<Example>(
E/flutter (21378):       create: (_) => Example(),
E/flutter (21378):       // we use `builder` to obtain a new `BuildContext` that has access to the provider
E/flutter (21378):       builder: (context, child) {
E/flutter (21378):         // No longer throws
E/flutter (21378):         return Text(context.watch<Example>().toString());
E/flutter (21378):       }
E/flutter (21378):     );
E/flutter (21378):   }
E/flutter (21378):   ```
E/flutter (21378): 
E/flutter (21378): If none of these solutions work, consider asking for help on StackOverflow:
E/flutter (21378): https://stackoverflow.com/questions/tagged/flutter
E/flutter (21378): 
E/flutter (21378): #0      Provider._inheritedElementOf (package:provider/src/provider.dart:343:7)
E/flutter (21378): #1      Provider.of (package:provider/src/provider.dart:293:30)
E/flutter (21378): #2      _SignUpFormState._saveForm (package:todo_app/screens/signup_form.dart:167:33)
E/flutter (21378): #3      _SignUpFormState._submitButton.<anonymous closure> (package:todo_app/screens/signup_form.dart:159:13)
E/flutter (21378): #4      _InkResponseState.handleTap (package:flutter/src/material/ink_well.dart:1154:21)
E/flutter (21378): #5      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:275:24)
E/flutter (21378): #6      TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:654:11)
E/flutter (21378): #7      BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:311:5)
E/flutter (21378): #8      BaseTapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:281:7)
E/flutter (21378): #9      GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:167:27)
E/flutter (21378): #10     GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:492:20)
E/flutter (21378): #11     GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:468:22)
E/flutter (21378): #12     RendererBinding.dispatchEvent (package:flutter/src/rendering/binding.dart:333:11)
E/flutter (21378): #13     GestureBinding._handlePointerEventImmediately (package:flutter/src/gestures/binding.dart:413:7)
E/flutter (21378): #14     GestureBinding.handlePointerEvent (package:flutter/src/gestures/binding.dart:376:5)
E/flutter (21378): #15     GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:323:7)
E/flutter (21378): #16     GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:292:9)
E/flutter (21378): #17     _invoke1 (dart:ui/hooks.dart:186:13)
E/flutter (21378): #18     PlatformDispatcher._dispatchPointerDataPacket (dart:ui/platform_dispatcher.dart:424:7)
E/flutter (21378): #19     _dispatchPointerDataPacket (dart:ui/hooks.dart:119:31)
E/flutter (21378): 
D/DecorView[](21378): onWindowFocusChanged hasWindowFocus false
I/FA      (21378): Application backgrounded at: timestamp_millis: 1693685303310

Kindly help


Course: Build and Publish Your First Mobile App Using Flutter - Learn Interactively
Lesson: StartApp Widget - Build and Publish Your First Mobile App Using Flutter

Hi @nkristianto !!
The error message you’re encountering indicates that Flutter is unable to find the correct Provider for the LoggedUser above your SignUpForm widget in the widget tree. This typically happens when the BuildContext you’re using to access the provider is not associated with the widget tree where the provider is defined.

Here are some common scenarios and solutions:

  1. Provider Placement: Ensure that you’ve wrapped your widget tree with the Provider<LoggedUser> at an appropriate level, typically in your main.dart or a higher-level widget that encompasses the entire application. For example:

    runApp(
      MultiProvider(
        providers: [
          ChangeNotifierProvider(create: (_) => LoggedUser()), // Your provider setup
        ],
        child: MyApp(), // Your root widget
      ),
    );
    
  2. Hot Reload vs. Hot Restart: If you recently added a new provider and performed a hot reload, try doing a hot restart instead. Hot reload may not update the widget tree correctly in some cases.

  3. Scope of the Provider: Make sure that the SignUpForm widget is indeed under the MultiProvider or Provider<LoggedUser> in the widget tree. Providers are scoped, and their scope determines which widgets can access them. If SignUpForm is in a different route or part of the widget tree, you may need to refactor your widget structure to ensure it’s under the correct provider scope.

  4. BuildContext: Ensure that the BuildContext used to access the provider is within the same scope where the provider is defined. If you’re creating a provider and trying to read it immediately, consider using the builder parameter when defining the provider to obtain a new BuildContext that has access to the provider.

Here’s an example of using the builder parameter with Provider:

Provider<LoggedUser>(
  create: (_) => LoggedUser(),
  builder: (context, child) {
    // Your widget tree that uses the provider
    return YourWidgetTree();
  },
)

By following these steps and ensuring the correct placement and scope of the provider, you should be able to resolve the issue and access Provider<LoggedUser> within your SignUpForm widget.
I hope it helps. Happy Learning :blush:

2 Likes

Hi @Javeria_Tariq
Thank you for your response.
That works, I could see the error is gone.
Thanks for your explanation, it is really helpful.


Course: Build and Publish Your First Mobile App Using Flutter - Learn Interactively
Lesson: Onboarding Screen - Build and Publish Your First Mobile App Using Flutter

2 Likes