Search code examples
flutterapidart

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


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


Solution

  • 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');