improve code

This commit is contained in:
nyne
2024-10-17 14:28:29 +08:00
parent c33f2c9918
commit e1e571052f
3 changed files with 148 additions and 90 deletions

View File

@@ -106,6 +106,22 @@ abstract class LoadingState<T extends StatefulWidget, S extends Object>
Future<Res<S>> loadData();
Future<Res<S>> loadDataWithRetry() async {
int retry = 0;
while (true) {
var res = await loadData();
if (res.success) {
return res;
} else {
if (retry >= 3) {
return res;
}
retry++;
await Future.delayed(const Duration(milliseconds: 200));
}
}
}
FutureOr<void> onDataLoaded() {}
Widget buildContent(BuildContext context, S data);
@@ -125,7 +141,7 @@ abstract class LoadingState<T extends StatefulWidget, S extends Object>
isLoading = true;
error = null;
});
loadData().then((value) async {
loadDataWithRetry().then((value) async {
if (value.success) {
data = value.data;
await onDataLoaded();
@@ -153,7 +169,7 @@ abstract class LoadingState<T extends StatefulWidget, S extends Object>
void initState() {
isLoading = true;
Future.microtask(() {
loadData().then((value) async {
loadDataWithRetry().then((value) async {
if (value.success) {
data = value.data;
await onDataLoaded();