skip to Main Content

I am trying to implement the below code block but User _user showing warning as:

Non-nullable instance field ‘_user’ must be initialized.
Try adding an initializer expression, or a generative constructor that initializes it, or mark it ‘late’.dartnot_initialized_non_nullable_instance_field

class _LandingPageState extends State<LandingPage> {
  User _user;
  @override
  Widget build(BuildContext context) {
    if (_user == null) {
      return const SigninScreen();
    }
    return Container();
  }
}

How to initialize this value?

I tried this but it’s not working.

User _user = null;

3

Answers


  1. In flutter/dart we don’t initialize variables as null using User _user = null; like in other languages.

    You can either do one of the following based on your need :

    • It can be declared null intially using ? sign like this User? _user;, now the handling of null works
    • If you know the variable will be initialized with a non null value before use, you can use keyword late like this late User _user; which will tell Dart compiler that it will be initialized before use, else you will get error during runtime
    Login or Signup to reply.
  2. As per your logic of checking if (_user == null) , There are chances that the value of variable _user will become null. But you have defined the variable as non nullable .
    Now, what you can do is just make the data type nullable by adding a ?

    By adding a ? at the end of a datatype, you are making the variable nullable.

    Please check the updated code

    class _LandingPageState extends State<LandingPage> {
      User? _user; //added ? at the end of DataType to make it nullable
      @override
      Widget build(BuildContext context) {
        if (_user == null) {
          return const SigninScreen();
        }
        return Container();
      }
    }
    

    additionally, You cant define the value of a variable like
    User user = null;
    while initializing, you can define like User? user; and the value of user will be null by default (you dont have to explicitly give null value while initializing).

    and whenever you want to modify the value of user and to make it null, you can try value assigning like user = null;

    Login or Signup to reply.
  3. What?

    You are close. The issue here is that you haven’t declared the _user variable to be nullable. This is done by putting a ? right after the Type.

    This is called "Sound Null Safety", you can read more about it here.

    How to apply this to your case

    When declaring _user you need to add a ? after User, like so:

    User? _user;
    

    Full example

    class _LandingPageState extends State<LandingPage> {
      User? _user;
      @override
      Widget build(BuildContext context) {
        if (_user == null) {
          return const SigninScreen();
        }
        return Container();
      }
    }
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search