简体   繁体   English

LateInitializationError:字段“_preferences@56516396”尚未初始化

[英]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.

相关问题 LateInitializationError:字段“mangaStatus”尚未在 flutter 中初始化 - LateInitializationError: Field 'mangaStatus' has not been initialized in flutter LateInitializationError:Flutter中的字段尚未初始化 - LateInitializationError: Field has not been initialized in Flutter flutter 错误 LateInitializationError:字段“@”尚未初始化 - flutter error LateInitializationError: Field '@' has not been initialized LateInitializationError:字段“chatRoomsStream”尚未在 Flutter 中初始化 - LateInitializationError: Field 'chatRoomsStream' has not been initialized in Flutter 未处理的异常:LateInitializationError:字段“_id@21016470”尚未初始化 - Unhandled Exception: LateInitializationError: Field '_id@21016470' has not been initialized LateInitializationError:即使使用 initState(),字段“animationController”也未初始化 - LateInitializationError: Field 'animationController' has not been initialized even using initState() flutter 错误 LateInitializationError: Field '_instance@21075166' has not been initialized - flutter error LateInitializationError: Field '_instance@21075166' has not been initialized 发生异常。 LateError(LateInitializationError:字段“注释”尚未初始化。) - Exception has occurred. LateError (LateInitializationError: Field 'notes' has not been initialized.) Clima 天气应用程序不工作它抛出 LateInitializationError: Field 'latitude' has not been initialized - Clima weather app not working it throws LateInitializationError: Field 'latitude' has not been initialized 在 FLutter 试图让 JSON 有这个问题:[ LateInitializationError: Field '_userData@577066488' has not been initialized] - In FLutter trying to get JSON had this problem: [ LateInitializationError: Field '_userData@577066488' has not been initialized]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM