Reputation: 41
I have tried to many days but getting null value on flutter side.
I have shared the code. Please let me know is there anything wrong?
I also refer the link How to access flutter Shared preferences on the android end (using java) but it it exact opposite my requirements.
MainActivity.java
SharedPreferences prefs = contextApplication.getSharedPreferences("preference", MODE_PRIVATE);
//save in shared prefs
prefs.edit().putString("hashMapEventsInBg_session", hashMapString).apply();
main.dart
SharedPreferences sp = await SharedPreferences.getInstance();
var demosession = sp.getString("hashMapEventsInBg_session");
Upvotes: 4
Views: 1333
Reputation: 12673
What you can do is:
getSharedPreferences
to "FlutterSharedPreferences"
.getSharedPreferences("FlutterSharedPreferences", MODE_PRIVATE)
"flutter."
in the key of any pair.putString("flutter.test", "Hello")
val prefs: SharedPreferences = getSharedPreferences("FlutterSharedPreferences", MODE_PRIVATE)
prefs.edit().putString("flutter.test", "Hello").apply()
String test = (await SharedPreferences.getInstance()).getString("test"); //returns Hello.
Upvotes: 3
Reputation: 5943
if you use android java
on android side
Map<String, String> data = yourdata;
SharedPreferences preferences = this.getSharedPreferences("FlutterSharedPreferences",MODE_PRIVATE);
JSONObject jsonObject = new JSONObject(message);
String jsonString = jsonObject.toString();
SharedPreferences.Editor editor = preferences.edit();
editor.putString("flutter.msg",jsonString);
editor.apply();
on flutter side
SharedPreferences preferences = await SharedPreferences.getInstance();
if(preferences.containsKey('msg')){
final message = preferences.getString('msg');
Map data = json.decode(message);
}
Upvotes: 2
Reputation: 1000
Package for using shared preference in flutter : https://pub.dev/packages/shared_preferences
To understand more about using shared preference in flutter : https://medium.com/flutterdevs/using-sharedpreferences-in-flutter-251755f07127
https://flutter.dev/docs/cookbook/persistence/key-value
Upvotes: 0