0

i have a problem with my ChangeNotifierProvider widget. my proivder cant be found so i cant update my target widget. there is similar errors that was solved but i cant understand the fixes as i am new on ChangeNotifier. So if you interested in help me, please tell clearly.

Here is my error: Error: Could not find the correct Provider above this GamePage 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 GamePage is under your MultiProvider/Provider. 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());
        }
      );
    }
    

and here is my Codes:
main.dart

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  Adverter.initialization();
  runApp(MyInheritor(child: YirmiDortteDokuz()));

class YirmiDortteDokuz extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '9in24 Game',
      theme: ThemeData(

      ),
      home: ChangeNotifierProvider( create: (BuildContext context) => Adverter(),
          child: MeetScreenPage()),
    );
  }
}

Here is the route to GamePage.dart from MeetScreenPage.dart. there is no ChangeNotifierProvider stuff here and it extends StatefulWidget:

Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) =>
              GamePage(step: 0, pushes: [0], isRetry: false)));

Here is consumer builder and consumer widget in GamePage.dart class. it extends StatefulWdiget too.

class GamePageState extends State<GamePage>{
 @override
 Widget build(BuildContext context) {
    Adverter _adverter = Provider.of<Adverter>(context, listen: false);
...
 Consumer<Adverter>(builder: (context, data, child) {
   return Text("Rewarded Point is ${_adverter.getrewardpoint()}");
 }),
}

And my Adverter.dart Class extends ChangeNotifier:

class Adverter extends ChangeNotifier{
 int _rewardedPoint = 0 ;
 int getrewardpoint() => _rewardedPoint;
...
 void showRewardedAd_second() {
    if (_rewardedAd == null) {
      return;
    }

    _rewardedAd.show(
        onUserEarnedReward: (RewardedAd ad, RewardItem rewardItem) {
          print("${rewardItem.amount} SANİYE KAZANILDI.");
          _rewardedPoint = _rewardedPoint + rewardItem.amount;

          notifyListeners();
        }
    );
    ...
  }
}

Please help and tell clearly to teach me. Thank you for your helps.

1 Answers1

1

You have to set the type of ChangeNotifierProvider to Adverter like:

home: ChangeNotifierProvider<Adverter>(
  create: (BuildContext context) => Adverter(),
  child: MeetScreenPage()),

If you still can't access the provider, you can try to move it above MaterialApp, for example:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  Adverter.initialization();
  runApp(MyInheritor(child: YirmiDortteDokuz()));

class YirmiDortteDokuz extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<Adverter>(
      create: (BuildContext context) => Adverter(),
      builder: (context, child) => MaterialApp(
        title: '9in24 Game',
        theme: ThemeData(),
        home: MeetScreenPage()),
    );
  }
}

Also, when defining your provider, use with instead of extends:

class Adverter with ChangeNotifier...
Peter Koltai
  • 8,296
  • 2
  • 10
  • 20
  • hello Peter. Thanks for your explaining and simple answer at first. i tried both of your solutions and second one is solve my problem little bit. i said "little bit" since it caused another problem. After i tried your second solution that makes MaterialApp child of ChangeNotifierProvider my correct error provider error has gone but interstitial and rewarded ads **display problem** has came instead of it. Interstitial and rewarded ads can't be shown although they are both loaded and this error doesn't be seen as output. Just cant be shown. ++ – kalfalarin_omer Oct 01 '22 at 10:57
  • ++ i can see them in other pages created with other classes but in GamePage class that i am using Notifier stuffs cant. i have waited for about 10 hours and tried again but same result. _if i remove notifier stuffs from everywhere, everything about admob service is normal._ So what is the reason of it and what must i do? My main problem is: updating reward and displaying it on GamePage. if there is another way from ChangeNotifier i can try. – kalfalarin_omer Oct 01 '22 at 10:57
  • Hi, unfortunately I don't precisely understand your current problem, I am not familiar with admob. But if you wan't to display something in GamePage that comes from another widget, ChangeNotifierProvider is one of the solutions. – Peter Koltai Oct 01 '22 at 11:20