Flutter StreamProvider是否在每次应用启动时加载所有数据?

huangapple go评论74阅读模式
英文:

Does flutter StreamProvider load all data on each app start

问题

我有一个Flutter应用程序,它使用流提供程序从Firebase加载数据并监听数据库的更改。
我的问题是,每次应用程序启动时数据是否从数据库加载?是否使用缓存?

class LocalMain extends StatelessWidget {
  const LocalMain({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        StreamProvider<ConfigModel>(
          create: (_) => locator<ConfigModel>().fetchModelsAsStream()
英文:

I have a flutter app which load data from firebase using stream provider and listen to changes on the db.
my question is does the data is loaded from db every time the app start ? does it use caching ?

class LocalMain extends StatelessWidget {
 const LocalMain({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
   return MultiProvider(
     providers: [
      StreamProvider&lt;ConfigModel&gt;(
        create: (_) =&gt; locator&lt;ConfigModel&gt;().fetchModelsAsStream()

答案1

得分: 1

将此内容翻译如下:

作为一个社区维基页面发布,以便他人参考。

正如@Frank van Puffelen所提到的

> locator&lt;ConfigModel&gt;().fetchModelsAsStream() 的缓存行为由其实现决定,我们无法从提供的代码中了解。

Firestore具有离线缓存功能,可以在离线时将数据存储在本地,并在联机时与服务器同步。当使用Firestore与StreamProvider时,此缓存可以通过在可用时使用缓存数据来提高性能和用户体验,从而减少不必要的网络请求。如果启用了Firestore的离线缓存,locator&lt;ConfigModel&gt;().fetchModelsAsStream() 在应用程序启动时可能会利用缓存数据,从而提高效率。然而,应考虑所需行为的具体实现细节和缓存设置。

有关Firestore离线缓存的更多信息,请访问此链接

英文:

Publishing this as a community wiki for others sake.

As mentioned by @Frank van Puffelen

> The behavior of locator&lt;ConfigModel\&gt;().fetchModelsAsStream() regarding caching is determined by its implementation, which we don't know from the provided code.

Firestore has an offline cache feature that stores data locally and synchronizes with the server when online. When using Firestore with StreamProvider, this cache can enhance performance and user experience by using cached data when available, reducing unnecessary network requests. If Firestore's offline cache is enabled, locator&lt;ConfigModel\&gt;().fetchModelsAsStream() will likely utilize the cached data when the app starts, improving efficiency. However, the specific implementation details and caching settings should be considered for desired behavior.

For more information about Firestore's offline cache please visit this link.

huangapple
  • 本文由 发表于 2023年7月20日 19:40:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76729468.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定