gpt4 book ai didi

dart - 从方法fetchData中删除异步修改器

转载 作者:行者123 更新时间:2023-12-03 03:10:21 27 4
gpt4 key购买 nike

我试图从存储库中获取数据,但是尝试调用api时出现错误,这是我的商店:

class Locations = _Locations with _$Locations;

abstract class _Locations implements Store {

ApiClient _apiClient;

_Locations(ApiClient apiClient){
_apiClient = apiClient;
}

@observable
List<Location>locations = [];

@action
Future<List<Location>> fetchLocations() async{
locations = await apiClient.getLocations();
}

}

错误输出是..
[SEVERE] mobx_codegen|mobx_generator on lib/ui/location/state/locations.dart:
Could not make class "Locations" observable. Changes needed:
1. Remove async modifier from the method "fetchLocations"

知道我在做什么错吗?

最佳答案

MobX.dart现在支持异步操作。只要确保返回Future即可。它会自动将所有突变包装在一个 Action 中!

@observable
ObservableList<Location> locations = [];

@action
Future<void> fetchLocations() async {
final locations = await _apiClient.getLocations();
locations.addAll(newLocations); // this will be wrapped inside an action
}

关于dart - 从方法fetchData中删除异步修改器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54735616/

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