How can I read the last change even if the program is closed and opened in the differences in the flutter program?

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

How can I read the last change even if the program is closed and opened in the differences in the flutter program?

问题

我正在开发一个Flutter项目。这个项目与API一起工作。我提供了在程序内部更改API URL的选项。当用户在程序中进行更改时,即使程序已打开并关闭,我希望能够读取最后一次更改。

我对Flutter还不太熟悉,所以我不知道。我是为了获取信息而提出这个问题。例如,我将值x赋给API变量。然后我在程序中使用y值代替x值。现在,我想在程序关闭和打开时读取每个x和y变量。我应该如何实现这一点?

如果我以C#为例,使用属性设置文件。

英文:

I am developing a project in flutter. this project works with api. I offer the option to change the api url from within the program. When the user makes changes in the program, even if the program is opened and closed, I want to read the last change.

I'm new to flutter so I don't know. I am asking this question for information. For example, I gave the value x to the api variable. Then I gave the y value instead of the x value in the program. Now I want to read every xaman y variable when the program is closed and opened. What is my way to do this?

If I give an example from C#, properties settings file

答案1

得分: 1

你可以使用shared_preferences包来实现这一点。

它们自己网页上稍微缩短的示例:

设置数值:

// 获取共享偏好设置。
final prefs = await SharedPreferences.getInstance();

// 保存一个字符串值到 'url' 键。
await prefs.setString('url', 'https://your.api.example.com');

稍后或在应用程序重新启动后读取该值:

// 获取共享偏好设置。
final prefs = await SharedPreferences.getInstance();

final String? url = prefs.getString('url');
英文:

You can use the shared_preferences package for this.

Slightly shortened example from their own page:

Setting the value:

// Obtain shared preferences.
final prefs = await SharedPreferences.getInstance();

// Save an String value to 'action' key.
await prefs.setString('url', 'https://your.api.example.com');

Reading the value later or after a app restart:

// Obtain shared preferences.
final prefs = await SharedPreferences.getInstance();

final String? url = prefs.getString('url');

答案2

得分: 0

你可以在Flutter中使用Hive,在写入或删除方面,Hive在性能上远远优于SQLite和SharedPreferences。

代码示例

var box = Hive.box('myBox');

box.put('urlName', 'url');

var name = box.get('urlName');

print('URL: $urlName');
英文:

You can use Hive in flutter, Hive greatly outperforms SQLite and SharedPreferences when it comes to writing or deleting.

code example

var box = Hive.box('myBox');

box.put('urlName', 'url');

var name = box.get('urlName');

print('URL: $urlName');

答案3

得分: 0

你可以将数据存储在设备本地存储中。有许多方法可以实现这一点。以下是您可以考虑的几种常用选项。

  1. 使用sharedPreference来存储数据。
    Shared Preference Package

  2. 如果您正在使用GetX状态管理,您可以使用getstorage。

  3. GetX Package Link

英文:

You can store data in device local storage.There are a lot of methods, you can achieve this thing. Here are couple of most use options you can consider.

  1. Use sharedPreference to store data.
    Shared Preference Package
  2. If you are using getx state management, you can use getstorage.
  3. Getx Package Link

huangapple
  • 本文由 发表于 2023年3月7日 15:03:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75658858.html
匿名

发表评论

匿名网友

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

确定