0

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.

xpetta
  • 607
  • 12
  • 28
  • Did you call init method with context? – p2kr Jun 17 '21 at 13:35
  • Do you mean to say where I'm trying to costume this `width: Device.width! * 0.9`? No haven't called the init method with context. But I have called it in the implementation class `class Device`. – xpetta Jun 17 '21 at 13:40
  • how do you expect to initialize static members without actually initializing them! Also init method creates object of Device every time its called. Use singleton pattern or convert it to static – p2kr Jun 17 '21 at 13:41
  • I'm new to programming, can you provide me the solution, please? – xpetta Jun 17 '21 at 13:44
  • I have added answer. If it works, mark it as answer to help others – p2kr Jun 17 '21 at 14:27

1 Answers1

1

Converting it to singleton class with parameters.

import 'package:flutter/material.dart';

class Device {
  late double height;
  late double width;
  late String orientationType;
  late String deviceType;

  static final Device _inst = Device._internal();
  Device._internal();

  factory Device({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;

    _inst.height = mediaQueryData.size.height - appBarHeight - statusBarHeight - bottomBarHeight;
    _inst.width = mediaQueryData.size.width;

    _inst.orientationType = (mediaQueryData.orientation == Orientation.portrait) ? "Portrait" : "Landscape";
    _inst.deviceType = (orientationType == "Portrait" && width! < 600) ? "Mobile" : "Tablet";

    return _inst;
  }
}

Use it like this:

Device(context: context).height

For more info on this topic visit:

p2kr
  • 606
  • 6
  • 18