Recipe-App---Flutter-UI icon indicating copy to clipboard operation
Recipe-App---Flutter-UI copied to clipboard

Support for Null-safety!

Open xscotophilic opened this issue 3 years ago • 2 comments

Some of the code doesn't work when we use Null-Safety.

For example (size_config.dart):

import 'package:flutter/widgets.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;
    // On iPhone 11 the defaultSize = 10 almost
    // So if the screen size increase or decrease then our defaultSize also vary
    defaultSize = orientation == Orientation.landscape
        ? screenHeight * 0.024
        : screenWidth * 0.024;
  }
}

ss

The non-nullable variable '_mediaQueryData' must be initialized. Try adding an initializer expression.

xscotophilic avatar Jun 12 '21 17:06 xscotophilic

Using late keyword will solve the issue! ss

xscotophilic avatar Jun 12 '21 18:06 xscotophilic

Because of null safety your variable must be initialzied when created, to fix this you can use late to tell it that it will be initialized after being created but before being used.

hafizn07 avatar Jun 27 '22 08:06 hafizn07

Yes, I was beginner at that time, now I understand.

xscotophilic avatar Oct 23 '22 11:10 xscotophilic