【发布时间】:2021-09-05 04:18:58
【问题描述】:
我有一个颤振应用程序,用户可以在其中将项目添加到存储在 firebase 中的列表中。用户一次最多可以添加 1000 个项目。最初这不是问题,但随着列表项的增加,应用程序变得越来越慢,直到在列表中大约 1000 个项目后一次添加多个项目时,由于内存使用,应用程序崩溃 -
线程 #10,名称 = 'io.flutter.1.ui',停止原因 = EXC_RESOURCE RESOURCE_TYPE_MEMORY(限制=1450 MB,未使用=0x0)
如何改进代码以提高性能。我想保留 Stream 的设置,因为它可以让我动态地过滤列表。这里还有一个信息是 WidgetA 和 WidgetB 也都使用 Stream Data 来显示列表中的列表项数。
为了便于阅读,我的代码做了一些简化:
主屏幕类:
Widget content(context) {
double h = MediaQuery.of(context).size.height; //screen height
double w = MediaQuery.of(context).size.width; //screen width
return StreamProvider<List<Activity>>.value(
catchError: (_, __) => null,
value: DatabaseService().activities(widget.uid),
builder: (context, snapshot) {
return SafeArea(
child: Container(
//color: Theme.of(context).backgroundColor, //SkyHookTheme.background,
child: Scaffold(
backgroundColor: Colors.transparent,
body: NotificationListener<ScrollNotification>(
onNotification: _handleScrollNotification,
child: Stack(children: [
ListView(
controller: _scrollController,
children: <Widget>[
Column(
children: <Widget>[
WidgetA(),
WidgetB(),
ActivityList(), //List of User Activities
],
)
],
),
]),
),
),
),
);
});
}
ActivityList类Listview构建:
ListView buildList(List<Activity> acts){
items = ListView.builder(
shrinkWrap: true,
physics: ClampingScrollPhysics(),
scrollDirection: Axis.vertical,
itemCount: len,
itemBuilder: (context, index) {
return ActivityTile(activity: acts[index], number: acts.length - (index));
},
);
return items;
}
任何提示/提示我可以如何改进这一点将不胜感激。
谢谢!
【问题讨论】:
标签: firebase performance flutter listview