Prefer typing uninitialized variables and fields. (prefer_typing_uninitialized_variables

Viewed 4032

I am declaring

static var screenHeight;
static var screenWidth;

Yet dart analysis is flagging it as Prefer typing uninitialized variables and fields. (prefer_typing_uninitialized_variables...

screenHeight = MediaQuery.of(context).size.height;
screenWidth = MediaQuery.of(context).size.width;
2 Answers

As you are not instantiating while declaring variables, it is often good practice to declare type beforehand. So just replace var with the explicit types. Replace var with double in this case.

static double screenHeight;
static double screenWidth;

Or, you can do this

static double screenHeight = MediaQuery.of(context).size.height;
static double screenWidth = MediaQuery.of(context).size.width;

The above marked answer is great if the variable is reciving same type of value everytime(like in case of OP) but if thats not the case then we can also fix the warning by using dynamic instaed of var.

dynamic variableName;
Related