From 088e31d80f10d4955f01b31029078c38bf02c565 Mon Sep 17 00:00:00 2001 From: Kingtous Date: Thu, 15 Sep 2022 11:06:44 +0800 Subject: [PATCH] fix: add null catch on address book request --- .../lib/desktop/pages/connection_page.dart | 2 +- flutter/lib/desktop/widgets/peer_widget.dart | 2 +- flutter/lib/models/ab_model.dart | 24 +++++++++++-------- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/flutter/lib/desktop/pages/connection_page.dart b/flutter/lib/desktop/pages/connection_page.dart index 134fe4219..b7aa92c0f 100644 --- a/flutter/lib/desktop/pages/connection_page.dart +++ b/flutter/lib/desktop/pages/connection_page.dart @@ -414,7 +414,7 @@ class _ConnectionPageState extends State { ); } else { if (model.abLoading) { - return Center( + return const Center( child: CircularProgressIndicator(), ); } else if (model.abError.isNotEmpty) { diff --git a/flutter/lib/desktop/widgets/peer_widget.dart b/flutter/lib/desktop/widgets/peer_widget.dart index 32976fb5b..9511fcec8 100644 --- a/flutter/lib/desktop/widgets/peer_widget.dart +++ b/flutter/lib/desktop/widgets/peer_widget.dart @@ -266,7 +266,7 @@ class AddressBookPeerWidget extends BasePeerWidget { loadEvent: 'load_address_book_peers', offstageFunc: (Peer peer) => !_hitTag(gFFI.abModel.selectedTags, peer.tags), - peerCardWidgetFunc: (Peer peer) => DiscoveredPeerCard( + peerCardWidgetFunc: (Peer peer) => AddressBookPeerCard( peer: peer, ), initPeers: _loadPeers(), diff --git a/flutter/lib/models/ab_model.dart b/flutter/lib/models/ab_model.dart index 18bb73c3f..2749e972f 100644 --- a/flutter/lib/models/ab_model.dart +++ b/flutter/lib/models/ab_model.dart @@ -28,21 +28,26 @@ class AbModel with ChangeNotifier { try { final resp = await http.post(Uri.parse(api), headers: await _getHeaders()); - Map 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']; + if (resp.body.isNotEmpty && resp.body.toLowerCase() != "null") { + Map 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']; + } + notifyListeners(); + return resp.body; + } else { + return ""; } - return resp.body; } catch (err) { abError = err.toString(); } finally { + notifyListeners(); abLoading = false; } - notifyListeners(); return null; } @@ -60,7 +65,6 @@ class AbModel with ChangeNotifier { return _ffi?.getHttpHeaders(); } - /// void addId(String id) async { if (idContainBy(id)) { return;