skip to Main Content

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

3

Answers


  1. 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');
    
    Login or Signup to reply.
  2. 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');
    
    Login or Signup to reply.
  3. 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
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search