【问题标题】:How can i store variable or to say list variable in the local storage in flutter?我如何在颤动的本地存储中存储变量或说列表变量?
【发布时间】:2021-01-15 02:49:38
【问题描述】:

我正在创建一个音乐播放器应用程序,它需要在用户从移动本地存储中选择音频文件后列出音乐磁贴。所以我需要将所有音乐文件路径存储在本地存储中,这样当用户从本地存储中选择一次音乐时,他/她不需要第二次选择。我不知道我该怎么做。我使用 file_picker 来选择文件和 path_provider 来存储本地数据。 请问您能帮我解决这种情况吗?

【问题讨论】:

    标签: flutter dart flutter-layout flutter-dependencies flutter-test


    【解决方案1】:

    您可以将其存储在共享首选项中,将 shared_preferences 插件添加到 pubspec.yaml 文件中::

    dependencies:
      flutter:
        sdk: flutter
      shared_preferences: "<newest version>"
    

    像这样保存数据:

    // obtain shared preferences
    final prefs = await SharedPreferences.getInstance();
    
    // set value
    prefs.setInt('counter', counter);
    
    

    最后使用 :

    读取数据
    final prefs = await SharedPreferences.getInstance();
    
    final counter = prefs.getInt('counter') ?? 0;
    

    注意:只能使用原始类型:int、double、bool、string 和 stringList。 它不是为存储大量数据而设计的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-25
      • 2013-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多