我试图从存储库中获取数据,但是尝试调用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
}

07-26 09:32