'Unhandled Exception: NoSuchMethodError: The method 'setString' was called on null
Through the application, I make inquiries from the database to get some previously stored data. I try to store id in myPreferences but I get error like that:
E/flutter (29170): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: NoSuchMethodError: The method 'setString' was called on null.
E/flutter (29170): Receiver: null
E/flutter (29170): Tried calling: setString("id", "6")
E/flutter (29170): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
E/flutter (29170): #1 MyPreferences.commit (package:flutter_apptestqeuriy/MyPreferences.dart:32:30)
E/flutter (29170): #2 AddCommentsState.getLogin.<anonymous closure> (package:flutter_apptestqeuriy/AddComment.dart:82:23)
E/flutter (29170): #3 State.setState (package:flutter/src/widgets/framework.dart:1240:30)
E/flutter (29170): #4 AddCommentsState.getLogin (package:flutter_apptestqeuriy/AddComment.dart:76:5)
E/flutter (29170): <asynchronous suspension>
E/flutter (29170): #5 AddCommentsState.initState.<anonymous closure> (package:flutter_apptestqeuriy/AddComment.dart:57:9)
E/flutter (29170): #6 interval.function (package:flutter_apptestqeuriy/AddComment.dart:21:9)
E/flutter (29170): #7 _rootRun (dart:async/zone.dart:1182:47)
E/flutter (29170): #8 _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter (29170): #9 _CustomZone.runGuarded (dart:async/zone.dart:997:7)
E/flutter (29170): #10 _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1037:23)
E/flutter (29170): #11 _rootRun (dart:async/zone.dart:1190:13)
E/flutter (29170): #12 _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter (29170): #13 _CustomZone.bindCallback.<anonymous closure> (dart:async/zone.dart:1021:23)
E/flutter (29170): #14 Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:18:15)
E/flutter (29170): #15 _Timer._runTimers (dart:isolate-patch/timer_impl.dart:397:19)
E/flutter (29170): #16 _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:428:5)
E/flutter (29170): #17 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)
E/flutter (29170):
I store the number through the following query:
Future<String> getLogin() async {
var response = await http.get(
Uri.encodeFull("http://xxxxxxxxx/ApplicationP.php"),);
setState(() {
var convertDataToJson = json.decode(response.body);
data = convertDataToJson['result'];
if (data != null) {
user_name = data[0]['id'];
_myPreferences.id = user_name;
_myPreferences.commit();
}
});
}
import 'package:shared_preferences/shared_preferences.dart';
class MyPreferences {
static const ID = "id";
static final MyPreferences instance = MyPreferences._internal();
static SharedPreferences _sharedPreferences;
String id = "";
MyPreferences._internal() {}
factory MyPreferences() => instance;
Future<SharedPreferences> get preferences async {
if (_sharedPreferences != null) {
return _sharedPreferences;
} else {
_sharedPreferences = await SharedPreferences.getInstance();
id = _sharedPreferences.getString(ID);
return _sharedPreferences;
}
}
Future<bool> commit() async {
await _sharedPreferences.setString(ID, id);
}
Future<MyPreferences> init() async {
_sharedPreferences = await preferences;
return this;
}
static Future<bool> clearPreference() async {
if (_sharedPreferences != null) {
_sharedPreferences.clear();
}
}
}
I followed up on some similar titles here but the problem is different.
I don't know if anyone knows a solution to the problem.
Solution 1:[1]
Your _sharedPreferences instance is not initialized.
Future<bool> commit() async {
_sharedPreferences ??= await SharedPreferences.getInstance();
await _sharedPreferences.setString(ID, id);
}
Solution 2:[2]
static final String TOKEN = "TOKEN";
SharedPreferences sharedPreferences;
setToken(String token) async {
sharedPreferences = await SharedPreferences.getInstance();
await sharedPreferences.setString(TOKEN, token);
}
getToken() async {
return _preferences.getString(TOKEN);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ravi Singh Lodhi |
| Solution 2 | Aravind B |
