admin管理员组文章数量:1356716
I'm currently learning Riverpod
and trying to implement the MVVM
(Model-View-ViewModel) pattern. I have a HomeModel
that holds two lists (animalList and birdList), both fetched asynchronously. I am using a HomeViewModel
to handle the fetching and updating of these lists.
Here is what I have so far:
@freezed
abstract class HomeModel with _$HomeModel {
const factory HomeModel({
@Default(AsyncData([])) AsyncValue<List<Animal>> animalList,
@Default(AsyncData([])) AsyncValue<List<Bird>> birdList,
}) = _HomeModel;
}
ViewModel (HomeViewModel):
@riverpod
class HomeViewModel extends _$HomeViewModel {
@override
HomeModel build() {
return HomeModel();
}
// Fetch the list of animals and update the state accordingly
Future<void> getAnimalList() async {
try {
final repository = ref.read(repositoryProvider);
state = state.copyWith(animalList: AsyncLoading());
final result = await repository.getAnimals();
state = state.copyWith(animalList: AsyncData(result));
} catch (e, stack) {
state = state.copyWith(animalList: AsyncValue.error(e, stack));
}
}
// Fetch the list of birds and update the state accordingly
Future<void> getBirdsList() async {
try {
final repository = ref.read(repositoryProvider);
state = state.copyWith(birdList: AsyncLoading());
final result = await repository.getBirds();
state = state.copyWith(birdList: AsyncData(result));
} catch (e, stack) {
state = state.copyWith(birdList: AsyncValue.error(e, stack));
}
}
}
The Issue:
In my HomeViewModel
, I have two separate methods (getAnimalList()
and getBirdsList()
) to fetch data asynchronously. What I am stuck on is how to call both methods when the HomeViewModel
is initialized. I want to make sure that both the animal and bird lists are fetched when the HomeViewModel
is created, but I'm not sure how to trigger both methods in the initialization phase.
Here is what I tried:
- Calling both methods from the build function, but it seems I can't
run async code directly in
build()
.
I'm currently learning Riverpod
and trying to implement the MVVM
(Model-View-ViewModel) pattern. I have a HomeModel
that holds two lists (animalList and birdList), both fetched asynchronously. I am using a HomeViewModel
to handle the fetching and updating of these lists.
Here is what I have so far:
@freezed
abstract class HomeModel with _$HomeModel {
const factory HomeModel({
@Default(AsyncData([])) AsyncValue<List<Animal>> animalList,
@Default(AsyncData([])) AsyncValue<List<Bird>> birdList,
}) = _HomeModel;
}
ViewModel (HomeViewModel):
@riverpod
class HomeViewModel extends _$HomeViewModel {
@override
HomeModel build() {
return HomeModel();
}
// Fetch the list of animals and update the state accordingly
Future<void> getAnimalList() async {
try {
final repository = ref.read(repositoryProvider);
state = state.copyWith(animalList: AsyncLoading());
final result = await repository.getAnimals();
state = state.copyWith(animalList: AsyncData(result));
} catch (e, stack) {
state = state.copyWith(animalList: AsyncValue.error(e, stack));
}
}
// Fetch the list of birds and update the state accordingly
Future<void> getBirdsList() async {
try {
final repository = ref.read(repositoryProvider);
state = state.copyWith(birdList: AsyncLoading());
final result = await repository.getBirds();
state = state.copyWith(birdList: AsyncData(result));
} catch (e, stack) {
state = state.copyWith(birdList: AsyncValue.error(e, stack));
}
}
}
The Issue:
In my HomeViewModel
, I have two separate methods (getAnimalList()
and getBirdsList()
) to fetch data asynchronously. What I am stuck on is how to call both methods when the HomeViewModel
is initialized. I want to make sure that both the animal and bird lists are fetched when the HomeViewModel
is created, but I'm not sure how to trigger both methods in the initialization phase.
Here is what I tried:
- Calling both methods from the build function, but it seems I can't
run async code directly in
build()
.
1 Answer
Reset to default 0You can simply change your build method to a future and run your codegen.
@riverpod
class HomeViewModel extends _$HomeViewModel {
@override
Future<HomeModel> build() async {
await getAnimalList();
await getBirdsList();
return HomeModel();
}
// Fetch the list of animals and update the state accordingly
Future<void> getAnimalList() async {
try {
final repository = ref.read(repositoryProvider);
state = state.copyWith(animalList: AsyncLoading());
final result = await repository.getAnimals();
state = state.copyWith(animalList: AsyncData(result));
} catch (e, stack) {
state = state.copyWith(animalList: AsyncValue.error(e, stack));
}
}
// Fetch the list of birds and update the state accordingly
Future<void> getBirdsList() async {
try {
final repository = ref.read(repositoryProvider);
state = state.copyWith(birdList: AsyncLoading());
final result = await repository.getBirds();
state = state.copyWith(birdList: AsyncData(result));
} catch (e, stack) {
state = state.copyWith(birdList: AsyncValue.error(e, stack));
}
}
}
本文标签:
版权声明:本文标题:How to call multiple asynchronous methods during ViewModel initialization with Riverpod in Flutter? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743967430a2570146.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
Iterable
– pskink Commented Mar 31 at 5:28