[英]LateInitializationError: Field '_preferences@56516396' has not been initialized
I think, the given error "LateInitializationError: Field '_preferences@56516396' has not been initialized.", is in this code.我认为,给定的错误“LateInitializationError:字段'_preferences@56516396'尚未初始化。”,在这段代码中。 I don't get the error correctly where is that, That's why I want your help to find it.
我没有正确地得到错误在哪里,这就是为什么我需要你的帮助来找到它。
import 'dart:convert';导入“飞镖:转换”;
import 'package:buis_talk/screens/profile/model/user.dart';
import 'package:shared_preferences/shared_preferences.dart';
class UserPreferences {
static late SharedPreferences _preferences;
static const _keyUser = 'user';
static const myUser = Usercard(
imageUrls:
'https://images.unsplash.com/photo-1554151228-14d9def656e4?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=333&q=80',
name: 'Sarah Abs',
email: 'sarah.abs@gmail.com',
about:
'Certified Personal Trainer and Nutritionist with years of experience in creating effective diets and training plans focused on achieving individual customers goals in a smooth way.',
profession: 'Entrepreneur',
interests: 'Stocks | Crypto | NFT\'s',
location: 'Delhi, India',
);
static Future init() async =>
_preferences = await SharedPreferences.getInstance();
static Future setUser(Usercard user)async {
final json = jsonEncode(user.toJson());
await _preferences.setString(_keyUser, json);
}
static Usercard getUsercard() {
final json = _preferences.getString(_keyUser);
return json == null ? myUser : Usercard.fromJson(jsonDecode(json));
}
}
This is the profile screen which is the main screen that display on the android screen.这是配置文件屏幕,它是显示在 android 屏幕上的主屏幕。
class ProfilePage extends StatefulWidget {
static const String routeName = '/profile';
const ProfilePage({ Key? key }) : super(key: key);
static Route route() {
return MaterialPageRoute(
settings: const RouteSettings(name: routeName),
builder: (context) => const ProfilePage(),
);
}
@override
State<ProfilePage> createState() => _ProfilePageState();
}
class _ProfilePageState extends State<ProfilePage> {
@override
Widget build(BuildContext context) {
final usercard = UserPreferences.getUsercard();
This is the second screen that is edit screen.这是编辑屏幕的第二个屏幕。
class EditProfilePage extends StatefulWidget {
const EditProfilePage({Key? key}) : super(key: key);
@override
_EditProfilePageState createState() => _EditProfilePageState();
}
class _EditProfilePageState extends State<EditProfilePage> {
Usercard user = UserPreferences.myUser;
// late Usercard user;
@override
void initState() {
super.initState();
user = UserPreferences.getUsercard();
}
Error could be on any screen from these but I don't know on which particular step it gives me error.错误可能出现在这些屏幕的任何屏幕上,但我不知道它在哪个特定步骤上给我错误。
This is the android screen.这是 android 屏幕。 The error shown like this on screen.
错误在屏幕上显示如下。
You dont use the init function. You need the use this in initState您不使用 init function。您需要在 initState 中使用它
@override
void initState() {
super.initState();
UserPreferences.init();
user = UserPreferences.getUsercard();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.