更新时间:2023-01-06 13:46:29
i在这里
中找到了解决方案 Flutter:启动时共享首选项为空
,请注意,如果您像在
之前那样在主类之外调用它,则
共享首选项将不起作用,您应该在initState()
内部调用它
i found the solution in here Flutter: Shared Preferences null on Startup and note that shared preference will not work if you called it outside the main class like i did before you should call it inside initState() this works
@override
void initState() {
super.initState();
readFile('phone');
SharedPreferences.getInstance().then((SharedPreferences sp) {
sharedPreferences = sp;
ph = sp.getString('phone');
setState(() {});
});
}