gpt4 book ai didi

How do I get mobx to update when I change a property in an ObservableList?(当我更改ObservableList中的属性时,如何让mobx更新?)

转载 作者:bug小助手 更新时间:2023-10-22 17:34:31 45 4
gpt4 key购买 nike



I currently have been using mobx for my flutter app, and I'm trying to update a ListTile to change it's colour onTap. Right now I have I have an ObservableList marked with @observable, and an @action that changes a property on an item in that list.

我目前一直在使用mobx作为我的flutter应用程序,我正在尝试更新ListTile以更改它在Tap上的颜色。现在我有一个标记为@observable的ObservableList,以及一个更改该列表中项目属性的@action。


class TestStore = TestStoreBase with _$TestStore;

abstract class TestStoreBase with Store {
final DataService _dataService;

TestStoreBase({
@required DataService dataService,
}) : assert(dataService != null),
_dataService = dataService,
players = ObservableList<Player>();

@observable
ObservableList<Player> players;

@action
Future<void> loadPlayers(User user) async {
final userPlayers = await _dataService.getUserPlayers(user);
players.addAll(userPlayers);
}

@action
void selectPlayer(int index) {
players[index].isSelected = !players[index].isSelected;
);
}
}

in my UI I have this inside of a listbuilder:

在我的UI中,我在列表生成器中有这样的内容:


return Observer(builder: (_) {
return Container(
color: widget.testStore.players[index].isSelected != null &&
widget.testStore.players[index].isSelected
? Colors.pink
: Colors.transparent,
child: ListTile(
leading: Text(widget.testStore.players[index].id),
onTap: () => widget.testStore.selectPlayer(index),
),
);
});

but it doesn't redraw when I call widget.testStore.selectPlayer(index);

但当我调用widget.testStore.selectPlayer(index)时,它不会重新绘制;


The second thing I tried was to add @observable in the 'Players' class on the isSelected bool, but it doesn't seem to work either.

我尝试的第二件事是在isSelected布尔的“Players”类中添加@observable,但似乎也不起作用。


@JsonSerializable()
class Player {
final String id;
final bool isUser;

@observable
bool isSelected;

Player(this.id, this.isUser, this.isSelected);

factory Player.fromJson(Map<String, dynamic> data) => _$PlayerFromJson(data);

Map<String, dynamic> toJson() => _$PlayerToJson(this);
}

any help would be greatly appreciated, thanks!

如有任何帮助,我们将不胜感激,谢谢!


更多回答
优秀答案推荐

Your are trying to take actions on the isSelected property, so basically you have to define the Player class as a MobX store as well to create a mixin that triggers reportWrite() on modifying isSelected.

您正在尝试对isSelected属性执行操作,因此基本上您必须将Player类定义为MobX存储,并创建一个在修改isSelected时触发reportWrite()的mixin。


Adding @observable annotation to players property only means to watch on the property itself, and typing players as a ObservableList means to watch on the list elements of the property, i.e. to watch on players[0], players[1]...and so on.

将@observable annotation添加到players属性仅意味着监视属性本身,而将players键入为ObservableList意味着监视该属性的列表元素,即监视players[0]、players[1]。。。等等


For example

例如


@JsonSerializable()
class Player = _Player with _$Player;

abstract class _Player with Store {
final String id;
final bool isUser;

@observable
bool isSelected;

_Player(this.id, this.isUser, this.isSelected);

factory _Player.fromJson(Map<String, dynamic> data) => _$PlayerFromJson(data);

Map<String, dynamic> toJson() => _$PlayerToJson(this);
}

Here is a similar issue from MobX's GitHub repo: https://github.com/mobxjs/mobx.dart/issues/129

以下是MobX的GitHub回购中的一个类似问题:https://github.com/mobxjs/mobx.dart/issues/129


更多回答

So for this case, marking the list with extension: .asObservale() doesn't work?

所以在这种情况下,用扩展名.asObservale()标记列表是行不通的?

@MarcoFerreira I'm afraid not, asObservable() is just a sugar syntax of ObservableList<T>.of(). See github.com/mobxjs/mobx.dart/blob/mobx-2.0.7/mobx/lib/src/api/….

@MarcoFerreira恐怕不是,因为Observable()只是ObservableList.of()的一个糖语法。请参阅github.com/mobxjs/mobx.dart/blob/mobx-2.0.7/mobx/lib/src/api/…。

45 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com