简体   繁体   English

如何在 flutter 存储中存储阵列

[英]How to store an Array in flutter storage

I some set of list coming from a server, which I would be using in multiple pages in my app.我有一组来自服务器的列表,我将在我的应用程序的多个页面中使用它们。 So I'm thinking of storing it in flutter storage or something to avoid loading the same content everytime in different pages.所以我正在考虑将它存储在 flutter 存储或其他东西中,以避免每次在不同页面中加载相同的内容。 So the issue is storing the array to flutter storage, which wouldn't work as it only stores string.所以问题是将数组存储到 flutter 存储中,因为它只存储字符串,所以它不起作用。

I dont know if there is any other approach to this or how can I convert to string and save, and how can I delete this item and add some other item as well.我不知道是否有任何其他方法或如何转换为字符串并保存,以及如何删除该项目并添加其他项目。

Your question is misleading.你的问题具有误导性。 What do you mean by Flutter Storage? Flutter 存储是什么意思?

To save data between screens we use a State Management approach, like Provider, Mobx, Bloc, etc. or even a simple statefull widget if that's enought for the use case.为了在屏幕之间保存数据,我们使用 State 管理方法,如 Provider、Mobx、Bloc 等,如果这对于用例来说足够的话,甚至可以使用简单的 statefull 小部件。

You can use the local app storage (take a look at https://pub.dev/packages/shared_preferences ).您可以使用本地应用程序存储(查看https://pub.dev/packages/shared_preferences )。

Your problem can be easily answer but you need to clarify your question.您的问题可以很容易地回答,但您需要澄清您的问题。 Most likelly what your are looking for is to create a simple class that stores the data inside a provider/mobx (you can pick other) and be access in your whole app:)最有可能您正在寻找的是创建一个简单的 class 将数据存储在提供程序/mobx 中(您可以选择其他)并在您的整个应用程序中访问:)

Update - Using Provider ( https://pub.dev/packages/provider ):更新- 使用提供程序( https://pub.dev/packages/provider ):

You said that you are using provider so I assume you have define it.你说你正在使用提供者,所以我假设你已经定义了它。 In your UI you can call Provider.of<yourDataModel>(context) to get the instance of your 'yourDataModel' class.在您的 UI 中,您可以调用Provider.of<yourDataModel>(context)来获取“yourDataModel”class 的实例 This can be used everywhere you do have a context (most of places but not in initState, for example) to acess or modify your data.这可以在您有上下文的任何地方使用(例如,大多数地方但不在 initState 中)来访问或修改您的数据。 Note: it you use it in a function use 'Provider.of(context, listen:false)' because a function doesn't need to rebuild it self when you data changes, only widgets should rebuild.注意:如果您在 function 中使用它,请使用“Provider.of(context, listen:false)”,因为 function 在数据更改时不需要自行重建,只有小部件应该重建。

You can store the array in a map as a JSON string in the flutter storage.您可以将阵列作为 flutter 存储中的 JSON 字符串存储在 map 中。 You can add and delete the item by using the map methods.您可以使用 map 方法添加和删除项目。

import 'dart:convert';

final storage = new FlutterSecureStorage();

Map<String, List<int>> map = {
    'key1': [1, 2, 3],
};

final mapEncoded = jsonEncode(map);
await storage.write(key: "mapKey", value: mapEncoded);
print(mapEncoded);
final mapDecoded = jsonDecode(storage.read(key: "mapKey"));
print(mapDecoded);

Output Output

{"key1":[1,2,3]}
{key1: [1, 2, 3]}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM