I have defined a class to retrieve the device info such as the width, height, orientation, and so on.
import 'package:flutter/material.dart';
class Device {
static double? height;
static double? width;
static String? orientationType;
static String? deviceType;
void init(BuildContext context) {
final mediaQueryData = MediaQuery.of(context);
final appBar = AppBar();
final appBarHeight = appBar.preferredSize.height;
final statusBarHeight = mediaQueryData.padding.top;
final bottomBarHeight = mediaQueryData.padding.bottom;
height = mediaQueryData.size.height - appBarHeight - statusBarHeight - bottomBarHeight;
width = mediaQueryData.size.width;
orientationType = (mediaQueryData.orientation == Orientation.portrait) ? "Portrait" : "Landscape";
deviceType = (orientationType == "Portrait" && width! < 600) ? "Mobile" : "Tablet";
}
}
When I try to calculate the size then as shown below I'm getting an error.
return Container(
width: Device.width! * 0.9,
);
Error message:
Null check operator used on a null value
But when I use it without multiplying width: Device.width
it works fine and need not have to insert the null
check operator as well.