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()