Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ProviderNotFoundException #63

Open
pitanni opened this issue Mar 31, 2024 · 1 comment
Open

ProviderNotFoundException #63

pitanni opened this issue Mar 31, 2024 · 1 comment

Comments

@pitanni
Copy link

pitanni commented Mar 31, 2024

Hello everyone, I have a problem with the library. I created an abstract class to handle connectivity across multiple pages in the same way. But I'm getting the error:

`════════ Exception caught by widgets library ═══════════════════════════════════
The following ProviderNotFoundException was thrown building StreamBuilder(state: _StreamBuilderBaseState<ConnectivityResult, AsyncSnapshot>#ab4f1):
Error: Could not find the correct Provider<StateStreamable<Object?>> above this BlocBuilder<StateStreamable<Object?>, Object?> Widget

This happens because you used a BuildContext that does not include the provider
of your choice. There are a few common scenarios:

  • You added a new provider in your main.dart and performed a hot-reload.
    To fix, perform a hot-restart.

  • The provider you are trying to read is in a different route.

    Providers are "scoped". So if you insert of provider inside a route, then
    other routes will not be able to access that provider.

  • You used a BuildContext that is an ancestor of the provider you are trying to read.

    Make sure that BlocBuilder<StateStreamable<Object?>, Object?> is under your MultiProvider/Provider<StateStreamable<Object?>>.
    This usually happens when you are creating a provider and trying to read it immediately.

    For example, instead of:

    Widget build(BuildContext context) {
      return Provider<Example>(
        create: (_) => Example(),
        // Will throw a ProviderNotFoundError, because `context` is associated
        // to the widget that is the parent of `Provider<Example>`
        child: Text(context.watch<Example>().toString()),
      );
    }
    

    consider using builder like so:

    Widget build(BuildContext context) {
      return Provider<Example>(
        create: (_) => Example(),
        // we use `builder` to obtain a new `BuildContext` that has access to the provider
        builder: (context, child) {
          // No longer throws
          return Text(context.watch<Example>().toString());
        }
      );
    }
    

If none of these solutions work, consider asking for help on StackOverflow:
https://stackoverflow.com/questions/tagged/flutter

The relevant error-causing widget was:
OfflineBuilder OfflineBuilder:file:///C:/..../glufri/lib/widgets/connectivity_widget.dart:9:41

When the exception was thrown, this was the stack:
#0 Provider._inheritedElementOf (package:provider/src/provider.dart:343:7)
#1 Provider.of (package:provider/src/provider.dart:293:30)
#2 ReadContext.read (package:provider/src/provider.dart:649:21)
#3 _BlocBuilderBaseState.initState (package:flutter_bloc/src/bloc_builder.dart:130:36)
#4 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:5611:55)
#5 ComponentElement.mount (package:flutter/src/widgets/framework.dart:5456:5)
#6 Element.inflateWidget (package:flutter/src/widgets/framework.dart:4335:16)
#7 Element.updateChild (package:flutter/src/widgets/framework.dart:3840:20)
#8 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5505:16)
#9 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5643:11)
#10 Element.rebuild (package:flutter/src/widgets/framework.dart:5196:7)
#11 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2904:19)
#12 WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:989:21)
#13 RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:448:5)
#14 SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1386:15)
#15 SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1311:9)
#16 SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:1169:5)
#17 _invoke (dart:ui/hooks.dart:312:13)
#18 PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:399:5)
#19 _drawFrame (dart:ui/hooks.dart:283:31)
`

@pitanni
Copy link
Author

pitanni commented Mar 31, 2024

My code of widgets/connectivity_widget.dart is:

`
import 'package:app_settings/app_settings.dart';
import 'package:flutter/material.dart';
import 'package:flutter_offline/flutter_offline.dart';

abstract class ConnectivityWidget extends StatelessWidget{
const ConnectivityWidget({Key? key}) : super(key: key);

@OverRide
Widget build(BuildContext context) => OfflineBuilder(
connectivityBuilder: (context, connectivity, child) =>
connectivity == ConnectivityResult.none ? disconnectedBuild(context) : child,
child: connectedBuild(context)
);

Widget connectedBuild(BuildContext context);

Widget disconnectedBuild(BuildContext context) => Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Icon(
Icons.wifi_off,
size: 128,
color: Colors.grey,
),
Text("Oh no!"),
Text("Nessuna connessione disponibile!"),
Padding(
padding: EdgeInsets.only(top: 24),
child: ElevatedButton(
onPressed: () => AppSettings.openAppSettings(),
child: Text("Impostazioni"),
),
),
],
),
),);
}
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant