【问题标题】:Could not find the correct Provider - Flutter找不到正确的提供者 - Flutter
【发布时间】:2019-12-23 15:09:47
【问题描述】:
  @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)
                              ],
                            ),
                          ],
                        ),
                      ),
                    ),
                  ),
                ),
              ),
            ),
          )
        ],
      ),
    );
  }

当我弹出 InApp() 类时,我尝试修改 numberOfCoins 变量。 但是我有以下错误:

[VERBOSE-2:ui_dart_state.cc(148)] 未处理的异常:错误:可能 在此 FantasyNbi 小部件上方找不到正确的提供程序

要修复,请:

  • 确保提供者是这个 FantasyNbi 的祖先 小部件 * 向 Provider 提供类型 * 向 Consumer * 向 Provider.of() 提供类型
  • 始终使用包导入。例如:import 'package:my_app/my_code.dart'; * Ensure the correct context` 是 正在使用中。

【问题讨论】:

标签: flutter dart flutter-provider


【解决方案1】:

您需要在ChangeNotifierProviderScaffold 之间建立一个builder 桥。

Provider 包已经有它自己的构建器,叫做Consumer,你可以像这样使用它:

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

查看此链接:https://pub.dev/packages/provider#reading-a-value

编辑:builder 现在是 create

【讨论】:

  • 在哪里使用此代码?我有同样的问题,但我知道如何插入 ChangeNotifierProvider 代码
  • @MuhammadWazexr 在哪里使用此代码并不重要。一切都是关于亲子关系。但通常我们将ChangeNotifierProvider 放在Router 中。
  • 还可以查看文档。很清楚为什么需要Consumerpub.dev/documentation/provider/latest/provider/…
【解决方案2】:

根据最新版本的Provider包,将ChangeNotifierProviderbuilder()方法改为create()。 所以编辑 Esen Mehmet 的版本,这将起作用:

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

【讨论】:

    【解决方案3】:
    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(),
            ));
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-02-19
      • 2021-04-04
      • 2021-01-06
      • 2019-12-05
      • 2023-01-26
      • 2021-06-09
      • 2021-08-09
      相关资源
      最近更新 更多