微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

更新列表/数组索引时Flutter Bloc状态不会更新状态

如何解决更新列表/数组索引时Flutter Bloc状态不会更新状态

我正在尝试在bloc类中实现复选框逻辑。为此,我在下面的类代码中创建了var embed1 = new discord.RichEmbed() .setTitle("hjgsadgv") message.channel.send(embed9) .then(function (message) { message.react("5️⃣") .then(() => message.react("4️⃣")) .then(() => message.react("3️⃣")) .then(() => message.react("2️⃣")) .then(() => message.react("1️⃣")) const filter = (reaction,user) => { return ['5️⃣','4️⃣','3️⃣','2️⃣','1️⃣'].includes(reaction.emoji.name) && user.id === message.author.id; } message.awaitReactions(filter,{ max: 1,time: 60000,errors: ['time'] }) .then(collected => { const reaction = collected.first(); if (reaction.emoji.name === '5️⃣') { message.reply('123'); } else { message.reply('321'); } var embed2 = new discord.RichEmbed() .setTitle("uysygadk") message.channel.send(embed10) }) }) 。当List<bool> checked事件触发时,状态第一次更新,我可以看到复选框处于选中状态。

CheckBoxClicked

但是在那之后,无论调用class CartProductsListBloc extends Bloc<CartProductsListEvent,CartProductsListState> { CartProductsListBloc() : super(InitialCartProductsListState()); final ProductRepository productRepository = ProductRepository(); List<bool> checked = List<bool>(); var productsList; @override Stream<CartProductsListState> mapEventToState( CartProductsListEvent event) async* { if (event is FetchCartProductsList) { yield FetchingInProgress(); try { productsList = await productRepository.loadlistofProductInUserCart(); //initialize checked according to productsList for (int i = 0; i < productsList.length; i++) { checked.add(false); } yield FetchCartProductsListSuccess( productsList: productsList,checked: checked); } catch (error) { yield FetchCartProductsListFail( error: 'Fail to laod data. Please try again\nor Report the issue'); } } if (event is CheckBoxClicked) { checked[event.index] = !checked[event.index]; yield CheckedBoxClickedHappened( productsList: productsList,checked: checked); } } } 的状态UI多少次都不会更新。当我热刷新时,出现更改,这意味着逻辑工作正常,但问题是Equatable无法检测到List / array索引中的更改。以下是CheckBoxClicked状态的代码

CheckedBoxClickedHappened

注意:我通过在bloc类中添加class CheckedBoxClickedHappened extends CartProductsListState { final productsList; final checked; const CheckedBoxClickedHappened({@required this.productsList,@required this.checked}); @override List<Object> get props => [productsList,checked]; } 解决此问题,并且每次触发int count = 0;事件时,我都会像下面那样更新count ++

CheckBoxClicked

并处于状态

if (event is CheckBoxClicked) {
          checked[event.index] = !checked[event.index];
          yield CheckedBoxClickedHappened(
              productsList: productsList,checked: checked,count: count++);
        }

该问题的目的是要知道,Equatable是否无法检测到List索引的更改,或者我做错了什么?如果有任何疑问,请询问我是否会改善。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。