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

时间:2020-08-18 06:53:03

标签: flutter-bloc equatable

我正在尝试在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索引的更改,或者我做错了什么?如果有任何疑问,请询问我是否会改善。

0 个答案:

没有答案