使用提供商的模型更新后,无状态小部件的孩子在模型更新后不会重建

发布于 2025-02-08 05:36:08 字数 1923 浏览 1 评论 0 原文

检查此github reposority ,您可以在其中找到所有 /em>所需的信息(代码,复制步骤,预期/实际结果,...)才能理解我的问题。
检查readme :)

此stackoverflow帖子地址什么是不起作用的?

但是,这是我问题的预览:

class ItemTile extends StatelessWidget {
  const ItemTile({
    Key? key,
    required this.item,
  }) : super(key: key);

  final ItemModel item;

  @override
  Widget build(BuildContext context) {
    return Provider(
      create: (context) => ItemProvider(item: item),
      child: ListTile(
        onTap: () => Navigator.push(
          context,
          ItemDetailsPage(onNotification: MyPageRoute.onNotification(context), item: item),
        ),
        title: const ItemTileTitle(),
      ),
    );
  }
}

class ItemTileTitle extends StatelessWidget {
  const ItemTileTitle({
    Key? key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final item = context.select<ItemProvider, ItemModel>((provider) => provider.item);

    return Text('ID: ${item.id}, Status: ${item.status}');
  }
}

这种当前的小部件配置将重建 itemtiletitle nesk iteg> itemtile tith 的父母重建不同/更新<代码>项目属性。

为什么这样?

我认为这是与小部件的生命周期相关的问题,以及 itemtile 如何不重建其子女 itemtiletitle ,因为没有什么在小部件树上确实发生了变化。只有提供商的值更改。
但是,为什么 context.select 构建 itemtiletitle的方法如果提供者的值改变了?

github存储库中的更多详细信息!

也请给我有关我写这篇文章的方式的反馈,如果我做错了什么!谢谢您的帮助! :)

Please check this github repository, you can find there all the information needed (code, steps to reproduce, expected/actual results, ...) to understand my problem.
Check the README :)

This StackOverflow post addresses the What is not working? section from the README file.

Nevertheless, here is a preview of my problem:

class ItemTile extends StatelessWidget {
  const ItemTile({
    Key? key,
    required this.item,
  }) : super(key: key);

  final ItemModel item;

  @override
  Widget build(BuildContext context) {
    return Provider(
      create: (context) => ItemProvider(item: item),
      child: ListTile(
        onTap: () => Navigator.push(
          context,
          ItemDetailsPage(onNotification: MyPageRoute.onNotification(context), item: item),
        ),
        title: const ItemTileTitle(),
      ),
    );
  }
}

class ItemTileTitle extends StatelessWidget {
  const ItemTileTitle({
    Key? key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final item = context.select<ItemProvider, ItemModel>((provider) => provider.item);

    return Text('ID: ${item.id}, Status: ${item.status}');
  }
}

This current widget configuration will not rebuild the ItemTileTitle whenever the ItemTile's parent rebuilds with a different/updated item property.

Why is that so?

I am thinking this is an issue related to the widget's lifecycle and how the ItemTile will not rebuild its child ItemTileTitle because nothing has really changed in the widget tree. Only the Provider's value changed.
But then why is the context.select from the build method of ItemTileTitle not rebuilding if the Provider's value changed?

More details in the github repository!: https://github.com/HeyShafty/ErrorNotificationListener

Also please give me feedback on the way I wrote this post if I am doing anything wrong! Thank you for helping! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暗藏城府 2025-02-15 05:36:08

使用提供程序。Value构造函数将成功触发重建。

@override
Widget build(BuildContext context) {
  return Provider.value(
    value: ItemProvider(item: item),
    child: ListTile(
      onTap: () => Navigator.push(
        context,
        ItemDetailsPage(onNotification: MyPageRoute.onNotification(context), item: item),
      ),
      title: const ItemTileTitle(),
    ),
  );
}

Using the Provider.value constructor will successfully trigger a rebuild.

@override
Widget build(BuildContext context) {
  return Provider.value(
    value: ItemProvider(item: item),
    child: ListTile(
      onTap: () => Navigator.push(
        context,
        ItemDetailsPage(onNotification: MyPageRoute.onNotification(context), item: item),
      ),
      title: const ItemTileTitle(),
    ),
  );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文