【问题标题】:Flutter Provider doesn't rebuild widgetFlutter Provider 不会重建小部件
【发布时间】:2019-12-09 00:21:50
【问题描述】:

这是我的一段代码, 但请在下结论之前阅读问题:

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(), 

【问题讨论】:

  • 你能和我们分享更多吗?我们需要提供者
  • 当然是为了这个人自己 :) 我将提供者剥离到与对象相关的内容,否则将是很多代码......请注意,在其他小部件中,提供者按预期工作,即使在这里它传递数据,也只是在刷新之前不会重建
  • 我更多的是考虑ChangeNotifierProvider。同样,您确定不会再次调用builder 方法吗?如果你在那里添加一个日志,它没有到达?
  • 帖子已更新...但请稍等...我可以多次“消费”,可以吗?
  • 你当然可以。这里是AppSnapshot 类型对象的另一个提供者吗?

标签: flutter dart flutter-provider


【解决方案1】:

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

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

Dart Provider 包的开发者也是explains the use caseFutureProvider

【讨论】:

    【解决方案2】:

    在 main.dart 中指定类型

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

    【讨论】:

    • 你认为这有什么影响?
    猜你喜欢
    • 2019-12-09
    • 1970-01-01
    • 1970-01-01
    • 2021-03-03
    • 2021-03-03
    • 2021-10-22
    • 2021-07-16
    • 1970-01-01
    • 2020-07-25
    相关资源
    最近更新 更多