通过该应用程序,我从数据库进行查询以获取一些以前存储的数据。我尝试将id存储在myPreferences中,但出现这样的错误:
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):
我通过以下查询存储号码:
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();
}
}
}
我在这里跟进了一些相似的标题,但问题有所不同。
我不知道是否有人知道该问题的解决方案。
答案 0 :(得分:1)
您的_sharedPreferences
实例未初始化。
Future<bool> commit() async {
_sharedPreferences ??= await SharedPreferences.getInstance();
await _sharedPreferences.setString(ID, id);
}