-1

This the is error console is showing. It says it's in Root widget

This is my code

'''

  initState: (_) async {
    Get.put<UserController>(UserController());
  }, 
  builder: (_) {
    if (Get.find<AuthController>().user?.isEmpty == true){
      return HomeScreen();
    } else {
      return Login();
    }
  }

'''

AuthController with firebase, i used Rxn for null safety.

'''

class AuthController extends GetxController {
  FirebaseAuth _auth = FirebaseAuth.instance;
  Rxn<User> _firebaseUser = Rxn<User>();

  String? get user => _firebaseUser.value?.email;

  @override
  void onInit() {
    _firebaseUser.bindStream(_auth.authStateChanges());
  }
}

'''

UserController

  Rx<UserModel> _userModel = UserModel().obs;

  UserModel? get user => _userModel.value;

  set user(UserModel? value) => this._userModel.value = value!;

  void clear(){
    _userModel.value = UserModel();
  }
Wai Yan
  • 11
  • 4

1 Answers1

1

You're forcing the nullable user into non-null using the !:

if (Get.find<AuthController>().user!.isEmpty){

Instead, null check it with ?

if (Get.find<AuthController>().user?.isEmpty == true){

You almost never want to force a nullable using ! unless you've already null checked it earlier in your code.

Nico Spencer
  • 940
  • 6
  • 11
  • thank you for the help Nico, i tried that before I posted this question and it was giving me this error **A nullable expression can't be used as a condition. Try checking that the value isn't 'null' before using it as a condition.dartunchecked_use_of_nullable_value** – Wai Yan Oct 20 '21 at 06:19
  • Oh right, You'll need to explicitly check against the boolean value you want to test against if it's nullable. I've updated the answer. – Nico Spencer Oct 20 '21 at 07:03
  • please help, it's still giving me the same error in the photo :( – Wai Yan Oct 20 '21 at 09:38
  • `class AuthController extends GetxController { FirebaseAuth _auth = FirebaseAuth.instance; Rxn _firebaseUser = Rxn(); String? get user => _firebaseUser.value?.email; @override void onInit() { _firebaseUser.bindStream(_auth.authStateChanges()); } }` This is my AuthController, if it helps – Wai Yan Oct 20 '21 at 10:12
  • please update your question and put it in a code block. can you also include your UserController? – Nico Spencer Oct 20 '21 at 11:24
  • it's updated, i'm so sorry for the late response, was busy with uni assignments – Wai Yan Oct 22 '21 at 00:32