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),
);
}
}
}
setUserbeforeupdateDbUser()? The error is about your callinguser!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 caseuserends up beingnulleven if you have promised that would not be the case.