/
loading_state_updater.dart
52 lines (49 loc) · 1.44 KB
/
loading_state_updater.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import 'package:flutter/material.dart';
import 'package:flutter_template/provider/core/loading_state_provider.dart';
import 'package:flutter_template/util/logger.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
void loadingStateUpdater({
required List<AsyncValue<dynamic>> asyncValueList,
required WidgetRef widgetRef,
required BuildContext context,
}) {
final notifier = widgetRef.watch(loadingStateProvider.notifier);
_asyncLogger(asyncValueList: asyncValueList, context: context);
_updater(notifier: notifier, asyncValueList: asyncValueList);
}
void _asyncLogger({
required List<AsyncValue<dynamic>> asyncValueList,
required BuildContext context,
}) {
for (final asyncValue in asyncValueList) {
asyncValue.when(
data: (data) => {
logger.i('success: ${asyncValue.runtimeType}: $data'),
},
error: (error, _) => {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
error.toString(),
),
),
),
logger.w('error: ${asyncValue.runtimeType}: $error'),
},
loading: () => {
logger.i('loading: ${asyncValue.runtimeType}'),
},
);
}
}
void _updater({
required LoadingState notifier,
required List<AsyncValue<dynamic>> asyncValueList,
}) {
for (final asyncValue in asyncValueList) {
if (asyncValue.isLoading) {
return notifier.show();
}
}
return notifier.hide();
}