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

@@ -167,6 +167,9 @@ class _PeersViewState extends State<_PeersView>
@override
Widget build(BuildContext context) {
// We should avoid too many rebuilds. MacOS(m1, 14.6.1) on Flutter 3.19.6.
// Continious rebuilds of `ChangeNotifierProvider` will cause memory leak.
// Simple demo can reproduce this issue.
return ChangeNotifierProvider<Peers>(
create: (context) => widget.peers,
child: Consumer<Peers>(builder: (context, peers, child) {
@@ -245,6 +248,9 @@ class _PeersViewState extends State<_PeersView>
: Container(child: visibilityChild);
}
// We should avoid too many rebuilds. Win10(Some machines) on Flutter 3.19.6.
// Continious rebuilds of `ListView.builder` will cause memory leak.
// Simple demo can reproduce this issue.
final Widget child = Obx(() => stateGlobal.isPortrait.isTrue
? ListView.builder(
itemCount: peers.length,