2

I keep getting this error on Flutter and I can't understand what is causing it. I am actually using some resources I found online. The app has a size_config file to get some basic info about the screen (code below). but for some reason the call keep returning null.

here is the code:

===============static_config.dart==================

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;
  }


double getProportionateScreenHeight(double inputHeight) {
  double screenHeight = SizeConfig.screenHeight;
  print(screenHeight.toString());
  return (inputHeight / 812.0) * screenHeight;
}

double getProportionateScreenWidth(double inputWidth) {
  double screenWidth = SizeConfig.screenWidth;
  return (inputWidth / 375.0) * screenWidth;
}

=====================home.dart===========================

import 'package:flutter/material.dart';
import 'size_config.dart';

class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: SizedBox(
        width: double.infinity,
        child: Padding(
          padding:
          EdgeInsets.symmetric(horizontal: getProportionateScreenWidth(20)),
          child: SingleChildScrollView(
            child: Column(
              children: [
                SizedBox(height: SizeConfig.screenHeight * 0.04),
                Text(
                  "Flutter Demo",
                  style: TextStyle(
                    color: Colors.black,
                    fontSize: getProportionateScreenWidth(28),
                    fontWeight: FontWeight.bold,
                  ),
                ),
                Text(
                  "Some random text here",
                  textAlign: TextAlign.center,
                ),
                SizedBox(height: SizeConfig.screenHeight * 0.08),

              ],
            ),
          ),
        ),
      ),
    );
  }
}

=============================== main.dart==================

import 'package:flutter/material.dart';
import 'home.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Demo',
      // home: SplashScreen(),
      // We use routeName so that we dont need to remember the name
      home: Home(),
    );
  }
}

Error

NoSuchMethodError: The method to double() is called on null Receiver : null Tried Calling : to double()

Shubhamhackz
  • 7,333
  • 7
  • 50
  • 71
Ktrel
  • 115
  • 1
  • 9

1 Answers1

1

The problem is you are trying to access the MediaQueryin init() method.You should call MediaQuery.of(context) inside build(BuildContext context) method.

Instead call your MediaQuery inside build.

import 'size_config.dart';

class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
   
_mediaQueryData = MediaQuery.of(context);
    screenWidth = _mediaQueryData.size.width;
    screenHeight = _mediaQueryData.size.height;
    orientation = _mediaQueryData.orientation;

    return SafeArea(
      child: SizedBox(
        width: double.infinity,
        child: Padding(
          padding:
          EdgeInsets.symmetric(horizontal: getProportionateScreenWidth(20)),
          child: SingleChildScrollView(
            child: Column(
              children: [
                SizedBox(height: SizeConfig.screenHeight * 0.04),
                Text(
                  "Flutter Demo",
                  style: TextStyle(
                    color: Colors.black,
                    fontSize: getProportionateScreenWidth(28),
                    fontWeight: FontWeight.bold,
                  ),
                ),
                Text(
                  "Some random text here",
                  textAlign: TextAlign.center,
                ),
                SizedBox(height: SizeConfig.screenHeight * 0.08),

              ],
            ),
          ),
        ),
      ),
    );
  }
}```
Shubhamhackz
  • 7,333
  • 7
  • 50
  • 71
  • Thanks for the suggestions. I took the code from a sample online and that is how they have it and their app compile no problem. I was trying to figure out why mine is not. Thanks again. – Ktrel Dec 20 '20 at 18:02
  • Update: I found the solution. You need to call : SizeConfig().init(context) on the starting screen of the entire app – Ktrel Jan 23 '21 at 07:10