简体   繁体   English

我应该如何从根本上提高 flutter 应用程序性能?

[英]How should I basically improve flutter application performance?

Okay, so this might be a long read for your, but may solve many problems for newbies like me.好吧,这对你来说可能是一篇很长的文章,但对于像我这样的新手来说可能会解决很多问题。 Ok now straight to the point, How exactly should I improve performance, while loading a bunch of list data from firestore and preventing the application from crashing.好了,现在开门见山,我究竟应该如何提高性能,同时从 firestore 加载一堆列表数据并防止应用程序崩溃。 Here is what happened with me, I'm developing a food delivery application and till now there are around 50 restaurants in this app, to what happened is, many of the low budget phones (specially vivo) didn't take the load and the app got crashed, so I limited the number of document to load at a time, but this actually affects the UX, there are competitors here which are way faster than our, I don't know what technology they've used but they're loading 98 restaurant in instant without any loading time and well I also tried to load data in the background, but the app crash with this to, So right now I'm stuck with limiting the number of document, but how can I improve?这是发生在我身上的事情,我正在开发一个送餐应用程序,直到现在这个应用程序中有大约 50 家餐厅,所发生的事情是,许多低预算手机(特别是 vivo)没有承受负荷,而且应用程序崩溃了,所以我限制了一次加载的文档数量,但这实际上影响了用户体验,这里有竞争对手比我们快得多,我不知道他们使用了什么技术但他们是在没有任何加载时间的情况下立即加载 98 家餐厅,我也尝试在后台加载数据,但是应用程序因此崩溃,所以现在我只能限制文档的数量,但我该如何改进? Currently the Homepage consists 10.network image cards, 3 asset image stack cards with gradient color and the list of 5 restaurants, which will be increased as the usee scrolls down but loading data isn't fast enough.目前首页有10张网络图卡,3张渐变颜色的资产图片堆叠卡和5家餐厅的列表,随着用户向下滚动会增加,但加载数据速度不够快。 Any one suggest me a way to speed it up or do I have to change the database?有人建议我加快速度的方法还是我必须更改数据库?

So, I think your app is slow because when something changes in the app, it renders all the widgets again and again, and it slows the app.所以,我认为你的应用程序很慢,因为当应用程序发生变化时,它会一次又一次地呈现所有小部件,并且会减慢应用程序的速度。 Some basic solutions are:一些基本的解决方案是:

1- Use const on the variables that never changes. 1- 对永不改变的变量使用const (For example: const Color colorPrimary = Color(0xffaabbcc)); (例如: const Color colorPrimary = Color(0xffaabbcc));

2- Use Provider package to load and store your data, so that you can use your data everywhere. 2-使用Provider package来加载和存储您的数据,以便您可以在任何地方使用您的数据。 Also, it prevents all widgets to render.此外,它会阻止所有小部件呈现。 It only renders the widget which uses that data.它只呈现使用该数据的小部件。 (For example, you don't want to render AppBar if you don't change your AppBar title everytime) (例如,如果您不每次都更改AppBar标题,则您不想渲染AppBar

3- Divide your large widgets into separate small widgets. 3- 将您的大部件分成单独的小部件。 It makes your project management easy.它使您的项目管理变得容易。

4- Maybe your images are big, and your app can't load them fast, then it crashes. 4-也许您的图像很大,并且您的应用程序无法快速加载它们,然后它就崩溃了。 Make their size smaller.使它们的尺寸更小。 (For example: 256x256 instead 512x512) (例如:256x256 而不是 512x512)

5- Use lazy loading when loading your data. 5- 加载数据时使用延迟加载。 Maybe your app tries to load all the data once, and it makes your app slow.也许您的应用程序尝试加载所有数据一次,这会使您的应用程序变慢。 With lazy load, most of the data won't be loaded until user scrolls down.使用延迟加载,在用户向下滚动之前不会加载大部分数据。 (I think you're already using it, but I just wanted to mention it) (我想你已经在使用它了,但我只是想提一下)

These are possible solutions.这些是可能的解决方案。

You can shrink your app if you're concerned about your application performance in release mode it uses different techniques to make your app more efficient by removing unused code from your app which is known as shrinking and many other techniques like Obfuscation which shorten the class names and methods, you can check this link for more information https://developer.android.com/studio/build/shrink-code .如果您担心应用程序在发布模式下的性能,您可以缩小应用程序,它使用不同的技术通过从应用程序中删除未使用的代码(称为缩小)和许多其他技术(例如缩短 class 名称的混淆)来提高应用程序的效率和方法,您可以查看此链接以获取更多信息https://developer.android.com/studio/build/shrink-code

Not sure if you still need an answer, but I believe your problem is related to loading all the views at once.不确定您是否还需要答案,但我相信您的问题与一次加载所有视图有关。

If your app crashes on startup only when you are loading too many results, most probably you are not using a ListView.builder to asynchronously load all the views.如果仅当您加载太多结果时您的应用程序在启动时崩溃,则很可能您没有使用ListView.builder来异步加载所有视图。

My advice for you would be the following:我对你的建议如下:

  1. Lazy load your UI using ListView.builder .使用ListView.builder延迟加载UI。 This will allocate memory only for the views visible on the screen.这将为屏幕上可见的视图分配 memory。
  2. Double check that you are loading the data only once.仔细检查您是否只加载一次数据。 You haven't share any code, but I ran into this issue once, where my Future was being called every time the state of the widget would rebuild.您没有共享任何代码,但我曾经遇到过这个问题,每次重建小部件的 state 时都会调用我的Future
  3. Use stateless widgets as much as possible.尽可能使用stateless小部件。 Split your widgets into reusable components and make as many const stateless widgets as possible.将您的小部件拆分为可重用的组件,并制作尽可能多的const无状态小部件。

If you want a detailed exampled you can check Tip number 4 in this article .如果您想要详细的示例,您可以查看本文中的技巧 4。

I did found a solution in 2020 itself, the reason for crash and slow speed were images, large images.我确实在 2020 年找到了解决方案,崩溃和速度慢的原因是图像,大图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何提高 BigQuery 中 ST_INTERSECT 的性能? - How to improve performance of ST_INTERSECT in BigQuery? Kube.netes 上的 Spark:如何提高我的表现? - Spark on Kubernetes: How to improve my performance? 如何提高 Firebase 实时数据库的性能 - How to improve performance in Firebase real time database 使用 Azure 提高 .Net 核心分析应用程序的性能 - Improve performance of .Net core analytics application using Azure 改进 CASE WHEN 性能 - Improve CASE WHEN Performance 如何提高代码性能(使用谷歌翻译 API) - How to improve code performance ( using Google Translate API ) 如何提高连接到 Amazon MQ 的 Lambda 函数的性能 - How to Improve Performance of Lambda Function which Connects to Amazon MQ 如何在Firebase中托管flutter应用? - How to host flutter application in Firebase? GCP:如何备份安装到文件存储的实例,或者基本上我想备份谷歌云平台中的文件存储 - GCP: How can I take backup of an instance that is mount to the file store or basically I want to take a backup of file store in google cloud platform Google BigQuery 中的分区会提高连接性能吗? - Will partitioning in Google BigQuery improve join performance?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM