feat: retry logic

Signed-off-by: Kingtous <kingtous@qq.com>
This commit is contained in:
Kingtous 2022-07-26 17:14:52 +08:00
parent d0e55f6f81
commit 8a3da4eb41
2 changed files with 40 additions and 15 deletions

View File

@ -609,6 +609,18 @@ class _ConnectionPageState extends State<ConnectionPage> {
builder: (context, snapshot) {
if (snapshot.hasData) {
return _buildAddressBook(context);
} else if (snapshot.hasError) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(translate("${snapshot.error}")),
TextButton(
onPressed: () {
setState(() {});
},
child: Text(translate("Retry")))
],
);
} else {
if (model.abLoading) {
return Center(
@ -616,7 +628,17 @@ class _ConnectionPageState extends State<ConnectionPage> {
);
} else if (model.abError.isNotEmpty) {
return Center(
child: Text(translate("${model.abError}")),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(translate("${model.abError}")),
TextButton(
onPressed: () {
setState(() {});
},
child: Text(translate("Retry")))
],
),
);
} else {
return Offstage();

View File

@ -24,22 +24,25 @@ class AbModel with ChangeNotifier {
notifyListeners();
// request
final api = "${await getApiServer()}/api/ab/get";
final resp = await http.post(Uri.parse(api), headers: await _getHeaders());
abLoading = false;
Map<String, dynamic> json = jsonDecode(resp.body);
if (json.containsKey('error')) {
abError = json['error'];
} else if (json.containsKey('data')) {
// {"tags":["aaa","bbb"],
// "peers":[{"id":"aa1234","username":"selfd",
// "hostname":"PC","platform":"Windows","tags":["aaa"]}]}
final data = jsonDecode(json['data']);
tags.value = data['tags'];
peers.value = data['peers'];
try {
final resp =
await http.post(Uri.parse(api), headers: await _getHeaders());
Map<String, dynamic> json = jsonDecode(resp.body);
if (json.containsKey('error')) {
abError = json['error'];
} else if (json.containsKey('data')) {
final data = jsonDecode(json['data']);
tags.value = data['tags'];
peers.value = data['peers'];
}
return resp.body;
} catch (err) {
abError = err.toString();
} finally {
abLoading = false;
}
print(json);
notifyListeners();
return resp.body;
return null;
}
Future<String> getApiServer() async {