Home > Net >  Null Check operator used on null value in Dart Flutter
Null Check operator used on null value in Dart Flutter

Time:08-02

Null check operator on null value error in Dart flutter which is caused by User user instance variable. Please help how to run this code to get value from firebase. Dummy user variables working fine till now. Code:

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../models/user.dart';
import '../services/database/users_database_services.dart';

class UserController extends GetxController {
  String userID = '';
  bool previouslyLoggedIn = false;
  UserDBService userDBService = UserDBService();

  User? user;
  setUser(String userID, String name, String phone, String email, int usercnic,
      String profilePictureLink) {
    this.userID = userID;
    user = User(
      email: email,
      name: name,
      phone: phone,
      userID: userID,
      usercnic: usercnic,
      profilePictureLink: profilePictureLink,
    );
  }

  updateDbUser() async {
    bool isSuccess = await userDBService.addUser(userID, user!.name,
        user!.phone, user!.email, user!.usercnic, user!.profilePictureLink);

    if (isSuccess) {
      Get.snackbar(
        "success",
        "User details updated successfully",
        snackPosition: SnackPosition.BOTTOM,
      );
    } else {
      Get.snackbar(
        "error",
        "User details not updated successfully",
        snackPosition: SnackPosition.BOTTOM,
        backgroundColor: Colors.red,
        colorText: Colors.white,
        duration: const Duration(seconds: 2),
      );
    }
  }
}

CodePudding user response:

The null check operator ! will treat the nullable value in front of it, a variable that may or may not be null, as non-nullable. In your case, this is user inside updateDbUser.

As mentioned in the comments, it is likely caused by calling updateDbUser before the user field is set. Since you already have a way to display failed updates, I would suggest simply setting isSuccess to false if there is no user:

updateDbUser() async {
  bool isSuccess = user != null && await userDBService.addUser(...);
  
  ...
}

This should ensure that user is not null, and if it is, it will simply set isSuccess to false and show you the red banner. Where possible, it is advisable to avoid the null check operator !, as it leads you to runtime exceptions/errors.

CodePudding user response:

Add null value condition in adding user like below or you can check if user is not empty

bool isSuccess = await userDBService.addUser(userID, user?.name ?? "",
    user?.phone ?? "", user?.email ?? "", user?.usercnic ?? 0, user?.profilePictureLink ?? "");

or

bool isSuccess = false;

if(user!= null)
{
   isSucess = await userDBService.addUser(userID, user!.name,
               user!.phone, user!.email, user!.usercnic, user!.profilePictureLink);
}

CodePudding user response:

your user data is null, check user data before update as below:

updateDbUser() async {
 bool isSuccess=false;
 if(user!=null){
isSuccess = await userDBService.addUser(userID, user!.name,
    user!.phone, user!.email, user!.usercnic, user!.profilePictureLink);
 }
if (isSuccess) {
  Get.snackbar(
    "success",
    "User details updated successfully",
    snackPosition: SnackPosition.BOTTOM,
  );
} else {
  Get.snackbar(
    "error",
    "User details not updated successfully",
    snackPosition: SnackPosition.BOTTOM,
    backgroundColor: Colors.red,
    colorText: Colors.white,
    duration: const Duration(seconds: 2),
  );
}
}
  • Related