Home > Software engineering >  My flutter code throws an exception about null variables. How to resolve or reformat it
My flutter code throws an exception about null variables. How to resolve or reformat it

Time:12-17

This is where the error comes from. "Null". In the console I received error like "type 'Null' is not a subtype of type 'String'"

 class UserPreferences {
  static late SharedPreferences _preferences;

  static const _keyUser = 'user';
  static const myUser = User(
    imagePath: "https://assets.teenvogue.com/photos/5591a26d33df629d5e314870/master/w_420,h_630,c_limit/08-jacob-morton.jpg",
    name: "Deva",
    email: "[email protected]",
    mobile: "9998887776",
    address: "2, Park street, Chennai",
    userid: "1",
    isDarkMode: false,
  );

  static Future init() async =>
      _preferences = await SharedPreferences.getInstance();

  static Future setUser(User user) async {
    final json = jsonEncode(user.toJson());

    await _preferences.setString(_keyUser, json);
  }

  static User getUser(){
    final json = _preferences.getString(_keyUser);

    return json == null ?  myUser : User.fromJson(jsonDecode(json));
  }
}
    }

Json getUser

Included the getUser class for clarification. Do we need to add dependecies or adds an additional property

class User{
  final String imagePath;
  final String name;
  final String email;
  final String mobile;
  final String address;
  final String userid;
  final bool isDarkMode;

  const User ({
    required this.imagePath,
    required this.name,
    required this.email,
    required this.mobile,
    required this.address,
    required this.userid,
    required this.isDarkMode,
});

  User copy({
    String? imagePath,
    String? name,
    String? email,
    String? mobile,
    String? address,
    String? userid,
    bool? isDarkMode,
}) =>
  User (
    imagePath: imagePath ?? this.imagePath,
    name:  name ?? this.name,
    email: email ?? this.email,
    mobile:  mobile ?? this.mobile,
    address: address ?? this.address,
    userid: userid ?? this.userid,
    isDarkMode: isDarkMode ?? this.isDarkMode,
  );

  static User fromJson(Map<String, dynamic>json) => User(
    imagePath: json ["imagePath"],
    name: json ["name"],
    email: json ["email"],
    mobile:  json ["mobile"],
    address: json ["address"],
    userid: json ["userid"],
    isDarkMode: json ["isDarkMode"],
  );

  Map<String, dynamic> toJson() => {
    "imagePath": imagePath,
    "name": name,
    "email": email,
    "mobile": mobile,
    "address": address,
    "isDarkMode": isDarkMode,
  };
}

Thanks in Advance for helping..
I am new to flutter and suggestions are welcome.

CodePudding user response:

You should try to give fallback values in the fromJson method in case some are null, like this

  static User fromJson(Map<String, dynamic>json) => User(
    imagePath: json ["imagePath"] ?? '',
    name: json ["name"] ?? '',
    email: json ["email"] ?? '',
    mobile:  json ["mobile"] ?? '',
    address: json ["address"] ?? '',
    userid: json ["userid"] ?? '',
    isDarkMode: json ["isDarkMode"] ?? false,
  );
  • Related