19
  @override
  Widget build(BuildContext context) {

    return WillPopScope(
      onWillPop: () async {
        return false;
      },
      child: Stack(
        children: <Widget>[
          DefaultTabController(
            length: 5,
            child: ChangeNotifierProvider(
              builder: (context) => MySchedule(),
              child: (
                  Scaffold(
                    appBar: AppBar(
                      actions: <Widget>[
                        Container(
                          width: MediaQuery.of(context).size.width,
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: <Widget>[
                              GestureDetector(
                                onTap: () async{
                                  await Navigator.of(context).push(
                                    MaterialPageRoute(builder: (context){
                                      return InApp();
                                    })
                                  );
                                  final MySchedule schedules = Provider.of<MySchedule>(context);
                                  schedules.numberOfCoins = 10;
                                },
                                child: Card(
                                  child: Padding(
                                    padding: const EdgeInsets.all(4.0),
                                    child: Row(
                                      children: <Widget>[
                                        Consumer<MySchedule>(
                                          builder: (context, coin, _) =>
                                              buildCoinBar(coin),
                                        ),
                                        SizedBox(
                                          width: 2,
                                        ),
                                        Stack(
                                          children: <Widget>[
                                            Image.asset('assets/coin2.png',
                                              height: 22, width: 22,),

                                          ],
                                        ),
                                      ],
                                    ),
                                  ),
                                ),
                              ),
                              Image.asset('assets/LOGO.png'),
                              Consumer<MySchedule>(
                                builder: (context, userdata, _) =>
                                    topRightElement(userdata, context),
                              ),
                            ],
                          ),
                        )
                      ],
                      automaticallyImplyLeading: false,
                      bottom: TabBar(
                        labelStyle: TextStyle(fontSize: 8),
                        tabs: [
                          Consumer<MySchedule>(
                            builder: (context, schedule, _) =>
                            buildNewCardNotification(schedule),
                          ),
                          Tab(icon: Icon(Icons.star), text: 'Csapatom' ,),
                          Tab(icon: Icon(Icons.verified_user), text: 'Forduló',),
                          Tab(icon: Icon(Icons.stars), text: 'Kártyáim',),
                          Tab(icon: Icon(Icons.account_balance), text: 'Ligák',),
                        ],
                      ),
                    ),
                    body: TabBarView(
                      children: [
                        Office(),
                        MyTeam(),
                        MatchListView(),
                        MyCardView(),
                        ChampionshipView2(),
                      ],
                    ),
                  )
              ),
            ),
          ),
          Visibility(
            visible: msgVisible,
            child: SafeArea(
              child: GestureDetector(
                onTap: (){
                  setState(() {
                    msgVisible = false;
                  });
                },
                child: Padding(
                  padding: const EdgeInsets.all(2.0),
                  child: ClipRRect(
                    borderRadius: BorderRadius.circular(0.0),
                    child: Card(
                      shape: RoundedRectangleBorder(
                          side: new BorderSide(color: Colors.lightGreenAccent, width: 2.0),
                          borderRadius: BorderRadius.circular(16.0)),
                      elevation: 8,
                      color: Colors.black87,
                      child: Container(
                        height: 64,
                        width: MediaQuery.of(context).size.width,
                        child: Row(
                          children: <Widget>[
                            Padding(
                              padding: const EdgeInsets.all(10.0),
                              child: Image.asset('assets/LOGO.png', height:44),
                            ),
                            Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                              children: <Widget>[
                                Text(title, style: TextStyle(
                                  fontSize: 16,
                                  color: Colors.lightGreenAccent
                                ),),
                                Text(body,
                                  overflow: TextOverflow.ellipsis)
                              ],
                            ),
                          ],
                        ),
                      ),
                    ),
                  ),
                ),
              ),
            ),
          )
        ],
      ),
    );
  }

I try to modify the numberOfCoins variable, when I pop the InApp() class. But I have the following error:

[VERBOSE-2:ui_dart_state.cc(148)] Unhandled Exception: Error: Could not find the correct Provider above this FantasyNbi Widget

To fix, please:

  • Ensure the Provider is an ancestor to this FantasyNbi Widget * Provide types to Provider * Provide types to Consumer * Provide types to Provider.of()
  • Always use package imports. Ex: import 'package:my_app/my_code.dart'; * Ensure the correct context` is being used.
Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
janosdupai
  • 519
  • 1
  • 5
  • 16
  • some one look at this this is the pain in ma *ss for a while now [enter link description here](https://stackoverflow.com/questions/66139465/another-exception-was-thrown-error-could-not-find-the-correct-providerappdata) – Muhammed Amein Feb 10 '21 at 15:12

3 Answers3

21

You need a builder bridge between ChangeNotifierProvider and Scaffold.

Provider package already has it's own builder called Consumer, you can use it like:

ChangeNotifierProvider<MySchedule>(
          create: (context) => MySchedule(),
          child: Consumer<MySchedule>(
                  builder: (context, provider, child) => Scaffold(....,

Check this link: https://pub.dev/packages/provider#reading-a-value

edit: builder is now create.

Mehmet Esen
  • 6,156
  • 3
  • 25
  • 44
  • Where to use this code? I have problem same, but idk how to insert the ChangeNotifierProvider code – Muhammad Wazexr Feb 03 '20 at 07:49
  • @MuhammadWazexr it doesn't matter where to use this code. Everything is about parent-child relationship. But usually we put `ChangeNotifierProvider` inside the `Router`. – Mehmet Esen Feb 03 '20 at 09:15
  • Also take a look at the docs. It is very clear why you need the `Consumer`. https://pub.dev/documentation/provider/latest/provider/Consumer-class.html – Er1 Feb 23 '20 at 09:34
15

According to the latest version of the Provider package, the builder() method of ChangeNotifierProvider was changed to create(). So editing Esen Mehmet's version, this will work instead:

ChangeNotifierProvider(
      create: (context) => MySchedule(),  //change builder to create
      child: Consumer<MySchedule>(
              builder: (context, provider, child) => Scaffold(....,
Peter Wauyo
  • 827
  • 1
  • 8
  • 11
1

You can use provider as Below,

class HomeApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return HomeAppState();
  }
}

class HomeAppState extends State<HomeApp> {
  final _user = UserModel();

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
        providers: [
          ChangeNotifierProvider<LoginNotifier>(create: (BuildContext context) {
            return LoginNotifier();
          }),
          ChangeNotifierProvider<UserModel>.value(value: _user),
        ],
        child: MaterialApp(
          debugShowCheckedModeBanner: false,
          theme: Provider.of<ThemeModel>(context).currentTheme,
          home: HomeScreen(),
        ));
  }
}

szelemeh
  • 56
  • 1
  • 8
Bhoomika Chauhan
  • 928
  • 1
  • 9
  • 11