0

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),
      );
    }
  }
}
2
  • Could you please add your error log here? Commented Aug 2, 2022 at 8:36
  • Are you sure you always call setUser before updateDbUser()? The error is about your calling user! which is an enforced cast from a nullable (User?) to non-nullable (User) type. The cast will put a runtime check which makes sure your application crashes in case user ends up being null even if you have promised that would not be the case. Commented Aug 2, 2022 at 8:37

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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);
}

5 Comments

Eh so if user ends up being null you want to create an empty row in the database?
That's also a solution like adding a user with a null check @julemand101
My question was meant to be about your code. You can see that if user are null, every field are being replaced to empty strings or 0 (expect for userID). That would rarely be something you actually want to happen.
Please check updated one @julemand101
You second example does not make much sense since we know that all the fields on a User are not null if you check the code from the question. So no need for the usage of ?? operator. Instead, I would add a local _user variable and check that against null since it would then promote the variable to a non-nullable type.
0

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),
  );
}
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.