'How to Fix The method '>=' was called on null. Receiver: null Tried calling: >=(25) in dart flutter
Guys, I am trying to create a BMI calculator app. everything is working fine but I am getting an error which is shown below while passing data in the condition statement. so can anyone please help me solve this issue it will help me a lot thank you :)
The following NoSuchMethodError was thrown building Builder(dirty):
The method '>=' was called on null.
Receiver: null
Tried calling: >=(25)
The relevant error-causing widget was:
MaterialApp file:///C:/Users/Prajesh/Desktop/Desktop/Flutter/bmi-calculator-flutter/lib/main.dart:8:12
When the exception was thrown, this was the stack:
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5)
#1 CalculatorBrain.getResult (package:bmi_calculator/bmi_brain.dart:17:14)
#2 _InputPageState.build.<anonymous closure>.<anonymous closure> (package:bmi_calculator/inputPage.dart:225:25)
#3 MaterialPageRoute.buildContent (package:flutter/src/material/page.dart:54:55)
#4 MaterialRouteTransitionMixin.buildPage (package:flutter/src/material/page.dart:107:27)
...
So basically I have created a class where all the logic is happening but when I try to initialize the method to a named parameter it gives an error.
Below is my code where I have done all the logic
import 'dart:math';
class CalculatorBrain {
CalculatorBrain({this.height, this.weight});
final int height;
final int weight;
double _bmi;
String calculateBMI() {
_bmi = weight / pow(height / 100, 2);
return _bmi.toStringAsFixed(1);
}
String getResult() {
if (_bmi >= 25) {
return 'Overweight';
} else if (_bmi > 18.5) {
return 'Normal';
} else {
return 'Underweight';
}
}
String getInterpretation() {
if (_bmi >= 25) {
return 'You have a higher than normal body weight. Try to exercise more.';
} else if (_bmi >= 18.5) {
return 'You have a normal body weight. Good job!';
} else {
return 'You have a lower than normal body weight. You can eat a bit more.';
}
}
}
And now I am using this class in the below code which is a front page of my app
import 'package:bmi_calculator/result_page.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'custom_card_child.dart';
import 'reusable_container.dart';
import 'Constant.dart';
import 'bottom_button.dart';
import 'bmi_brain.dart';
enum Gender { male, female, }
class InputPage extends StatefulWidget {
@override
_InputPageState createState() => _InputPageState();
}
class _InputPageState extends State<InputPage> {
Gender selectedGender;
int height = 180;
int weight = 60;
int age = 20;
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
appBar: AppBar(
title: Text('BMI CALCULATOR'),
centerTitle: true,
elevation: 10.0,
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Expanded(child: Row(
children: [
Expanded(child: reusableContainer(
onPress: (){
setState(() {
selectedGender = Gender.male;
});
},
colour: selectedGender == Gender.male ? Kactiverang : Kinactiverang,
cardChild: CustomCardChild(
icon: FontAwesomeIcons.mars,
text: 'MALE',
),
),),
Expanded(child: reusableContainer(
onPress: (){
setState(() {
selectedGender = Gender.female;
});
},
colour: selectedGender == Gender.female ? Kactiverang : Kinactiverang,
cardChild: CustomCardChild(
icon: FontAwesomeIcons.venus,
text: 'FEMALE',
),
),),
],
),
),
Expanded(child: reusableContainer(
colour: Kactiverang,
cardChild: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'HEIGHT',
style: KtextStyle,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.baseline,
textBaseline: TextBaseline.alphabetic,
children: [
Text(
height.toString(),
style: KnumTextStyle,
),
Text(
'cm'
)
],
),
SliderTheme(
data: SliderTheme.of(context).copyWith(
thumbShape: RoundSliderThumbShape(enabledThumbRadius: 15.0),
overlayShape: RoundSliderOverlayShape(overlayRadius: 30.0),
thumbColor: Color(0xFFEB1555),
activeTrackColor: Colors.white,
inactiveTrackColor: Color(0xFF8D8E98),
overlayColor: Color(0x29EB1555)
),
child: Slider(
value: height.toDouble(),
min: 120.0,
max: 230.0,
//activeColor: Color(0xFFEB1555),
//inactiveColor: Color(0xFF8D8E98),
onChanged: (double newValue){
setState(() {
height = newValue.round();
});
},
),
),
],
),
),),
Expanded(child: Row(
children: [
Expanded(child: reusableContainer(
colour: Kactiverang,
cardChild: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'WEIGHT',
style: KtextStyle,
),
Text(
weight.toString(),
style: KnumTextStyle,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
RoundIconButton(
onPress: (){
setState(() {
weight--;
});
},
icon: FontAwesomeIcons.minus,
),
SizedBox(width: 10.0,),
RoundIconButton(
onPress: (){
setState(() {
weight++;
});
},
icon: FontAwesomeIcons.plus,
),
],
)
],
),
)),
Expanded(child: reusableContainer(
colour: Kactiverang,
cardChild: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'AGE',
style: KtextStyle,
),
Text(
age.toString(),
style: KnumTextStyle,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
RoundIconButton(
onPress: (){
setState(() {
age--;
});
},
icon: FontAwesomeIcons.minus,
),
SizedBox(width: 10.0,),
RoundIconButton(
onPress: (){
setState(() {
age++;
});
},
icon: FontAwesomeIcons.plus,
),
],
)
],
),
)),
],
),
),
Bottom_button(
onPress: (){
CalculatorBrain myBMI = CalculatorBrain(height: height, weight: weight);
//CalculateBMI myBMI = CalculateBMI();
Navigator.push(context,
MaterialPageRoute(builder: (context) => ResultPage(
result: myBMI.getResult(),
remark: myBMI.getInterpretation(),
bmiValue: myBMI.calculateBMI(),
)));
},
button_titile: 'CALCULATE',
),
],
),
),
);
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
class RoundIconButton extends StatelessWidget {
RoundIconButton({@required this.onPress, @required this.icon});
final Function onPress;
final IconData icon;
@override
Widget build(BuildContext context) {
return RawMaterialButton(
onPressed: onPress,
child: Icon(icon),
elevation: 6.0,
fillColor: Color(0xFF4C4F5E),
shape: CircleBorder(),
constraints: BoxConstraints.tightFor(width: 56.0, height: 56.0),
);
}
}
and I want to pass these data to my next page which is given below
import 'package:bmi_calculator/Constant.dart';
import 'package:bmi_calculator/reusable_container.dart';
import 'package:flutter/material.dart';
import 'bottom_button.dart';
class ResultPage extends StatelessWidget {
final String result;
final String bmiValue;
final String remark;
// ResultPage({@required this.bmiValue, @required this.remark, @required this.result});
ResultPage({this.bmiValue, this.remark, this.result});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('BMI CALCULATOR'),
centerTitle: true,
),
body: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Expanded(
child: Container(
padding: EdgeInsets.all(15.0),
alignment: Alignment.bottomLeft,
child: Text('Your Result', style: KresultTextStyle,),
),
),
Expanded(
flex: 5,
child: reusableContainer(
colour: Kactiverang,
cardChild: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(result.toUpperCase(), style: KnormalTextStyle,),
Text(bmiValue, style: KBMItextStyle,),
Text('all well',
style: KremarkTextStyle,
textAlign: TextAlign.center,
),
],
),
),
),
Bottom_button(onPress: (){Navigator.pop(context);}, button_titile: 'RE-CALCULATE'),
],
),
);
}
}
So, can anyone please help me to solve this issue or at least tell me that why I am getting this error? Thank you :)
Solution 1:[1]
You are calling getResult() before calculateBMI(), where your _bmi is null hence giving the exception.
You can solve it by either initialising the value but it wont fix the value in the result page.
You might want to calculate the Bmi first and save it and then pass it. like this.
onPress: (){
CalculatorBrain myBMI = CalculatorBrain(height: height, weight: weight);
final val = mBmi.calculateBMI();
Navigator.push(context,
MaterialPageRoute(builder: (context) => ResultPage(
result: myBMI.getResult(),
remark: myBMI.getInterpretation(),
bmiValue: val,
)));
},
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Sahdeep Singh |