non-nullable variable is not initialized

Ahmed Al-Khateeb

I am having this problem where it says the non nuablle variable must be initialized

enter image description here

  static MediaQueryData _mediaQueryData;
  static double screenWidth;
  static double screenHeight;
  static double deafualtSize;
  static Orientation orientation;

  void init(BuildContext context) {
    _mediaQueryData = MediaQuery.of(context);
    screenWidth = _mediaQueryData.size.width;
    screenHeight = _mediaQueryData.size.height;
    orientation = _mediaQueryData.orientation;
  }
}
Siddharth Agrawal

Ig you need to do something like this

 static MediaQueryData _mediaQueryData = 0;
  static double screenWidth = 0;
  static double screenHeight = 0;
  static double deafualtSize = 0;
  static Orientation? orientation;
  
  void init(BuildContext context) {
    _mediaQueryData = MediaQuery.of(context);
    screenWidth = _mediaQueryData.size.width;
    screenHeight = _mediaQueryData.size.height;
    orientation = _mediaQueryData.orientation;
  }
}

Everythime you use the orientation variable you will have to call it like orientation!
This error is because of the new null safety feature. You have to either give a default value or say that the variable can be null by using a ? after the type declaration as in the example above
Hope it helps. Let me know if there is any other problem

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Value of a non-initialized variable in C++

Kotlin property declared as non-nullable is nullable even if it has initialized value

How to convert nullable variable to non nullable type?

C initialized and non initialized array with variable size

Non-nullable instance field must be initialized

Future variables and null safety: Non-nullable instance field 'notifications' must be initialized

error: Non-nullable instance field 'screenSize' must be initialized

Non-nullable instance field must be initialized and Unhandled error LateInitializationError

Non-nullable instance field '_selectedDate' must be initialized

dart says "non-nullable variable must be initialized before use", even after assigning with if, else block

"Non-nullable instance field '_items' must be initialized.\nTry adding an initializer it

The non-nullable variable '_preferences' must be initialized. Try adding an initializer expression

Non-nullable instance field '_selectedSize' must be initialized

How to fix this error "The non-nullable variable '_mediaQueryData' must be initialized. "

error: Non-nullable instance field 'result' must be initialized

Flutter-Non-nullable instance field ‘{0}’ must be initialized error

Non-nullable instance field '_localizedStrings' must be initialized

Non-nullable instance field '_repository' must be initialized

Non-nullable instance field ''" must be initialized

Non-nullable instance field ['controller'] must be initialized in flutter

Non-nullable instance field 'screenHeight' must be initialized on creating a constructor. What does that mean?

Non nullable instance field 'verificationId' must be initialized

I have an error like the non-nullable variable 'sessionUser' must be initialized

Non-nullable instance field '_dio' must be initialized Flutter

Non-nullable instance field 'Welcome' must be initialized Flutter

Non-nullable instance field '_controller' must be initialized

Convert a nullable to a non-nullable variable in C#/dotnet

Non-nullable instance field 'httpRequestService' must be initialized

Non-nullable instance of SharedPreferences must be initialized in Flutter