颤抖的消费者未返回正确的数据

问题描述

所以我有一个相对简单的小部件树:

Widget GetUserData(){

    return ChangeNotifierProvider(
      create: (context) => new UserDog.empty(),child: FutureBuilder<UserDog>(
        future: _getUserData,builder: (_,AsyncSnapshot<UserDog> snapshot){

          return Consumer<UserDog>(
            builder: (_,dog,__){

              if (snapshot.hasData){
                dog = snapshot.data;
              }

              return CreateMainContainer();
            }
          );
        },),);
}

Widget CreateMainContainer(){

    return Consumer<UserDog>(
        builder: (dogContext,__) {

          print(dog.imageID); // Is null here but isn't in the Consumer above this widget

          return SafeArea(
            child: Padding(
              padding: EdgeInsets.fromLTRB(
                  MediaQuery.of(dogContext).size.width * 0.07,MediaQuery.of(dogContext).size.width * 0.07,0),child: Column(
                  children: <Widget>[
                    Flexible(flex: 15,child: CreateNameSection()),Flexible(flex: 35,child: UserDogWidget()),Flexible(flex: 25,child: CreateDetailsSection())
                  ]),)
        );
      }
    );
}

future从数据库返回有效数据,并正确填充UserDog对象,在Consumer内的GetUserData()中,dog变量返回正确填充的UserDog对象。但是由于某种原因,Consumer中的CreateMainContainer返回了一个空的UserDog对象。 我对Flutter相当陌生,所以我可能在做一些愚蠢的事情,但是我不知道为什么会这样。在重构之前,这种方法可以正常工作,但是我无法弄清楚为什么对象没有沿小部件树传播?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)