-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpbin_usecase.dart
38 lines (32 loc) · 1.14 KB
/
httpbin_usecase.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import 'package:async/async.dart';
import 'package:flutter_template/data/repository/httpbin_repository.dart';
import 'package:flutter_template/provider/api/httpbin/httpbin_repository_provider.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
part 'httpbin_usecase.g.dart';
abstract class _HttpbinUseCase {
Future<void> postHttpBin(String id);
Future<void> errorHttpBin();
}
@riverpod
class HttpbinUseCase extends _$HttpbinUseCase implements _HttpbinUseCase {
HttpBinRepository get _httpBinRepository =>
ref.read(httpbinRepositoryProvider);
@override
FutureOr<void> build() {}
final AsyncCache<dynamic> _postCache = AsyncCache.ephemeral();
@override
Future<void> postHttpBin(String id) async {
state = const AsyncLoading();
state = await AsyncValue.guard(
() => _postCache.fetch(() => _httpBinRepository.postHttpBin(id)),
);
}
final AsyncCache<dynamic> _errorCache = AsyncCache.ephemeral();
@override
Future<void> errorHttpBin() async {
state = const AsyncLoading();
state = await AsyncValue.guard(
() => _errorCache.fetch(() => _httpBinRepository.errorHttpBin()),
);
}
}