找不到正确的提供者-Flutter [英] Could not find the correct Provider - Flutter

查看:348
本文介绍了找不到正确的提供者-Flutter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@override
Widget build(BuildContext context){

@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变量。
但我遇到以下错误:

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)]未处理的异常:错误:
不能在FantasyNbi窗口小部件上方找到正确的提供者

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

要修复,请:


  • 确保提供者是FantasyNbi
    小部件的祖先*向提供者提供类型*向
    消费者提供类型*向Provider.of()提供类型


    • 始终使用程序包导入。例如: import‘package:my_app / my_code.dart’; *确保使用的是正确的上下文`。

    • 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 correctcontext` is being used.


      推荐答案

      您需要在 ChangeNotifierProvider之间建立 builder 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(....,
      

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

      编辑: builder 现在是 create

      这篇关于找不到正确的提供者-Flutter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆