1

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;
  }
}
6
  • The error you are facing is not in the code you shows. You have a non nullablle parameter somewhere (with attribute @required). Commented Aug 23, 2021 at 15:39
  • This is weird as I still have not used them anywhere and it already shows the error Commented Aug 23, 2021 at 15:48
  • Can you post your stacktrace. You can add that after your question.. Commented Aug 23, 2021 at 15:49
  • I have added it Commented Aug 23, 2021 at 15:52
  • Can you post the class those are inside? THe code you have posted is to small to unddertand Commented Aug 23, 2021 at 15:54

1 Answer 1

2

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

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

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.