Flutter Provider不重建小部件

时间:2019-07-31 13:06:40

标签: flutter dart flutter-provider

这是我的一部分代码, 但请先阅读问题,然后再得出结论:

class RescheduleCard extends StatelessWidget {

@override
  Widget build(BuildContext context)=> Consumer<AppSnapshot>(
    builder: (context, appSnapshot, _)=>
    (appSnapshot.reschedule!=null)
    ?RescheduleBuilder(
          model: appSnapshot.reschedule,
          controllers: appSnapshot.controllers,
        )
    :Carouselcard(
      title:  carouselPageTitle(title: rescheduleTitle,test: appSnapshot.test),
      colors: yellows,

所以我是提供商的新手,我习惯了Bloc, 我的api一旦收到一些调用提供程序并设置了模型;

编辑:这是提供商256条线路跳到了您所关注的问题 “重新安排” ...

class AppSnapshot with ChangeNotifier {

  RescheduleModel _reschedule;
  RescheduleModel get reschedule => _reschedule;

  void cleanReschedule() {
    reschedule=null;
    notifyListeners();
  }

  set reschedule(RescheduleModel reschedule) {
    _reschedule=reschedule;
    notifyListeners();
  }
}

重新编辑:一切之上:

void main() async {
  final AppSnapshot _appSnapshot = AppSnapshot();
  await _appSnapshot.load();
  runApp(ChangeNotifierProvider(
          builder: (context) => _appSnapshot,
          child: Initializer()));
}

我希望“消费者”能够重建我的小部件, 但是没有!

我确定数据在那里,因为小部件位于转盘内 并且一旦我移动它,它就会正确重建。

我想念什么? 谢谢

重新编辑:这是另一个轮播卡的示例,提供商在该卡上工作得很顺利,更改会实时应用

 Consumer<AppSnapshot>(
    builder: (context, appSnapshot, _)=> Carouselcard(
      title: carouselPageTitle(title: optionsTitle,test: appSnapshot.test),
      colors: lightBlues,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          ((appSnapshot.id??'')!='')                                                        // ID WIDGET
            ? ListTile(
              leading: CustomIcon(
                icon: deleteIcon,
                onTap: () => appSnapshot.deleteID(),
              ),
              title: _customCard(onTap:()=> _showID(id: appSnapshot.id,context: context)),
              subtitle: Text(tap2delete,textScaleFactor:0.8),
            )
            : IdTile(), 

2 个答案:

答案 0 :(得分:1)

根据此multiprocessing libraryFutureProvider不会监听更改,它只会在Future完成后重建Consumers 1次。

文章甚至解释了作者如何对此进行测试。

Dart Provider程序包的开发人员也为FutureProvider article

答案 1 :(得分:0)

指定main.dart中的类型

void main() async {
final AppSnapshot _appSnapshot = AppSnapshot();
await _appSnapshot.load();
runApp(ChangeNotifierProvider< **AppSnapshot** >(
      builder: (context) => _appSnapshot,
      child: Initializer()));
}