Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access flutter Shared preferences on the android end (using java)

I have the following function in dart to set a particular boolean preference value.

_switchPersistentNotifications() {
  setState(() {
    isPersistentNotificationEnabled = !isPersistentNotificationEnabled;
  });
  widget.preferences.setBool(
      "isPersistentNotificationEnabled", isPersistentNotificationEnabled);
}

This function sets the value of isPersistentNotificationEnabled preference.

Now on the native android end, I am supposed to use this shared preference value. Here's what I have done so far.

SharedPreferences preferences =
                    PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                // check if the state change notification is to be shown
                if (preferences.getBoolean("flutter.isStateChangeNotificationEnabled", false)) {
                    showConnectionStateChangeNotification();
                }

And the if condition never gets evaluated to true. I also tried printing all the existing preference values using the code below.

Map<String, ?> allEntries = preferences.getAll();
            for (Map.Entry<String, ?> entry : allEntries.entrySet()) {
                Log.d("map values", entry.getKey() + ": " + entry.getValue().toString());
            }

And it only presents the values that the Android has created (using java).

Any help in accessing the preference values is greatly appreciated. Thank You.

like image 454
sujay_br Avatar asked Mar 18 '19 15:03

sujay_br


People also ask

How do I access my shared preferences on flutter?

To use the shared_preferences plugin, we have to import it into our file: import 'package:shared_preferences/shared_preferences. dart'; The shared_preferences plugin exports a SharedPreferences class, which has methods that can be used to set data of various primitive types in SharedPreferences.

Where are shared preferences stored Android?

Android Shared Preferences Overview Android stores Shared Preferences settings as XML file in shared_prefs folder under DATA/data/{application package} directory. The DATA folder can be obtained by calling Environment.

How do I access SharedPreferences?

These preferences will automatically save to SharedPreferences as the user interacts with them. To retrieve an instance of SharedPreferences that the preference hierarchy in this activity will use, call getDefaultSharedPreferences(android. content. Context) with a context in the same package as this activity.

Can we use SharedPreferences in Android?

Shared Preferences allow you to save and retrieve data in the form of key,value pair. In order to use shared preferences, you have to call a method getSharedPreferences() that returns a SharedPreference instance pointing to the file that contains the values of preferences.

How to add shared preferences in flutter?

Adding Shared Preferences to Flutter. Shared preferences will store data locally on the device, so for example if you want to use some data in a different page then you can use the Shared Preferences to store and retrieve that data. So first to add it you need to navigate to the pubspec.yaml file, and write the following:

How to store data locally in a flutter application?

In this article, we will use the shared preferences plugin to store data locally in a Flutter application. First we need to create a Flutter project, after following the documentation and installing the Flutter SDK. You can then open vscode or android studio and execute in the terminal the following command:

How do I start using sharedpreferences?

To begin using SharedPreferences you must first get an instance of the SharedPreferences class that’s connected to your app’s on-disk config file. Call the getInstance () static method to load and parse the file into a SharedPreferences instance. This is an asynchronous method so use the await keyword inside an async function to streamline access.

How do I remove data from shared preferences?

To remove any data from Shared Preferences you need to use the method remove (), for example: 1 2 3 4 Future<bool> removePayment() async{ SharedPreferences preferences = await SharedPreferences.getInstance(); return preferences.remove("payment"); }


1 Answers

Sorry this answer is coming late, just got the answer from flutter github page.

What you can do to accomplish this is:

SharedPreferences prefs = getSharedPreferences("FlutterSharedPreferences", MODE_PRIVATE);

then if you want to read e.g. "myValue" key, you have to add "flutter." prefix:

String value = prefs.getString("flutter."+key, null);
like image 150
KelvinROLEX Avatar answered Oct 09 '22 05:10

KelvinROLEX