Flutter Provider 不会重建小部件

标签 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()));
}

我期待“消费者”重建我的小部件,
但没有!

我确定数据在那里,因为小部件在旋转木马内
一旦我移动它,它就会正确重建。

我错过了什么?
谢谢你

重新编辑:这是另一张 Carousel Card 的示例,其中提供程序快速工作并且实时应用更改
 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(), 

最佳答案

根据这个article FutureProvider不监听变化,它只会重建 Consumers 1 次,当 Future 完成时。

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

Dart Provider 包的开发者还有 explains the use caseFutureProvider .

关于Flutter Provider 不会重建小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57291471/

相关文章:

dart - 对齐 Flutter scrollview 的第一个小部件最初居中,但所有后续选定的小部件也在中心

Flutter 提供程序状态管理、注销概念

flutter - 使用Provider是否意味着没有Stateful Widgets?

flutter - 滑动动画结束后如何只渲染pageView页面

FLUTTER - 如何防止 RiveAnimationController 在播放结束时重置?

Flutter,如何在有状态小部件中正确声明和使用变量?

dart - 如何在Angular Dart 2中编写自定义模式验证器

google-maps - Flutter - 谷歌地图折线模式不起作用

firebase - 为什么Firebase返回已删除的帐户?

使用Provider时出现Flutter 'No Overlay widget exists above EditableText'错误