You can use late
keyword as you are in null safety and you want to initialize the fields latter on.
By declaring a non-nullable late variable, we promise that it will be non-null at runtime, and Dart helps us with some compile-time guarantees.
But I recommend to only use late sparingly, and to always initialize late variables when they are declared.
import 'package:flutter/material.dart';
class SizeConfig {
static late MediaQueryData _mediaQueryData;
static late double screenWidth;
static late double screenHeight;
static late double defaultSize;
static late Orientation orientation;
void init(BuildContext context) {
_mediaQueryData = MediaQuery.of(context);
screenWidth = _mediaQueryData.size.width;
screenHeight = _mediaQueryData.size.height;
orientation = _mediaQueryData.orientation;
}
}
And here another thing is if you want to make your fields nullable
then you cam make theme nullable
by using ?
after the type like bellow.
import 'package:flutter/material.dart';
class SizeConfig {
static MediaQueryData? _mediaQueryData;
static double? screenWidth;
static double? screenHeight;
static double? defaultSize;
static Orientation? orientation;
void init(BuildContext context) {
_mediaQueryData = MediaQuery.of(context);
screenWidth = _mediaQueryData?.size.width;
screenHeight = _mediaQueryData?.size.height;
orientation = _mediaQueryData?.orientation;
}
}