refact: flutter, ChangeNotifier, reduce rebuild (#9392)

Signed-off-by: fufesou <linlong1266@gmail.com>
This commit is contained in:
fufesou
2024-09-19 18:48:01 +08:00
committed by GitHub
parent 88a99211f3
commit c6e3f60a6b
4 changed files with 21 additions and 7 deletions

View File

@@ -184,10 +184,17 @@ class PeerTabModel with ChangeNotifier {
notifyListeners();
}
// `notifyListeners()` will cause many rebuilds.
// So, we need to reduce the calls to "notifyListeners()" only when necessary.
// A better way is to use a new model.
setCurrentTabCachedPeers(List<Peer> peers) {
Future.delayed(Duration.zero, () {
final isPreEmpty = _currentTabCachedPeers.isEmpty;
_currentTabCachedPeers = peers;
notifyListeners();
final isNowEmpty = _currentTabCachedPeers.isEmpty;
if (isPreEmpty != isNowEmpty) {
notifyListeners();
}
});
}