Reputation: 631
I have done the following codes and no dart files are showing error but an error is thrown when I process it. The error is Unexpected null value
The Codes are as follows :
main.dart
import 'package:bmi_calculator/output_page.dart';
import 'package:flutter/material.dart';
import 'input_page.dart';
import 'output_page.dart';
import 'package:flutter/services.dart';
void main() => runApp(BMICalculator());
class BMICalculator extends StatelessWidget {
@override
Widget build(BuildContext context) {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
return MaterialApp(
theme: ThemeData.dark().copyWith(
primaryColor: Color(0xff111428),
scaffoldBackgroundColor: Color(0xff111428),
shadowColor: Color(0xFFEEF1F9),
),
initialRoute: '/',
routes: {
'/': (context) => InputPage(),
'/first': (context) => OutputPage(),
},
);
}
}
input_page.dart
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';
import 'package:flutter/rendering.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'container_cards.dart';
import 'first_line_containers.dart';
import 'package:bmi_calculator/container_cards.dart';
import 'package:bmi_calculator/constants.dart';
import 'package:bmi_calculator/calculator_brain.dart';
enum Gender {
male,
female,
}
class InputPage extends StatefulWidget {
@override
_InputPageState createState() => _InputPageState();
}
class _InputPageState extends State<InputPage> {
Gender? selectedGender;
int heightStartValue = 180;
int weightStartValue = 60;
int ageStartValue = 21;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text(
'BMI CALCULATOR',
),
),
body: Column(
children: [
Expanded(
child: Row(
children: [
Expanded(
child: ContainerCard(
onPress: () {
setState(() {
selectedGender = Gender.male;
});
},
myColor: selectedGender == Gender.male
? kActiveBoxColors
: kInactiveBoxColors,
myCardDetails: FirstLineContainers(
myStrings: "MALE",
),
),
),
Expanded(
child: ContainerCard(
onPress: () {
setState(() {
selectedGender = Gender.female;
});
},
myColor: selectedGender == Gender.female
? kActiveBoxColors
: kInactiveBoxColors,
myCardDetails: FirstLineContainers(
myIcons: Icon(FontAwesomeIcons.female),
myStrings: "FEMALE",
),
),
),
],
),
),
Expanded(
child: ContainerCard(
myColor: kInactiveBoxColors,
myCardDetails: FittedBox(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'HEIGHT',
style: kTextStyles,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
textBaseline: TextBaseline.alphabetic,
crossAxisAlignment: CrossAxisAlignment.baseline,
children: [
Text(
heightStartValue.toString(),
style: kNumberStyles,
),
SizedBox(width: 3),
Text(
'Cms',
style: kTextStyles,
textAlign: TextAlign.end,
)
],
),
SliderTheme(
data: SliderTheme.of(context).copyWith(
activeTrackColor: kActiveTrackColor,
inactiveTrackColor: kInactiveTrackColor,
thumbColor: kThumbColor,
overlayColor: kOverlayColor,
thumbShape: kThumbShape,
overlayShape: kOverlayShape,
valueIndicatorShape: kValueIndicatorShape,
valueIndicatorColor: kValueIndicatorColor,
),
child: Slider(
value: heightStartValue.toDouble(),
min: 80,
max: 240,
divisions: 160,
label: "$heightStartValue",
onChanged: (double changedValue) {
setState(() {
heightStartValue = changedValue.round();
});
},
),
),
],
),
),
),
),
Expanded(
child: Row(
children: [
Expanded(
child: ContainerCard(
myColor: kInactiveBoxColors,
myCardDetails: FittedBox(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('WEIGHT', style: kTextStyles),
Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.baseline,
textBaseline: TextBaseline.alphabetic,
children: [
Text(
weightStartValue.toString(),
style: kNumberStyles,
),
SizedBox(width: 3),
Text(
'KGs',
style: kTextColor,
)
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
CustomButton(
iconMarker: FontAwesomeIcons.minus,
afterPress: () {
setState(() {
weightStartValue--;
});
},
),
SizedBox(
width: 20,
),
CustomButton(
iconMarker: FontAwesomeIcons.plus,
afterPress: () {
setState(() {
weightStartValue++;
});
},
),
],
),
],
),
),
),
),
Expanded(
child: ContainerCard(
myColor: kInactiveBoxColors,
myCardDetails: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'AGE',
style: kTextStyles,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.baseline,
textBaseline: TextBaseline.alphabetic,
children: [
Text(
ageStartValue.toString(),
style: kNumberStyles,
),
SizedBox(width: 3),
Text(
'Yrs',
style: kTextColor,
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
CustomButton(
afterPress: () {
setState(() {
ageStartValue--;
});
},
iconMarker: FontAwesomeIcons.minus),
SizedBox.fromSize(
size: Size(20, 0),
),
CustomButton(
afterPress: () {
setState(() {
ageStartValue++;
});
},
iconMarker: FontAwesomeIcons.plus),
],
),
],
),
),
),
],
),
),
BottomButtons(
buttonText: 'Calculate Your BMI',
onTap: () {
CalculatorBrain calc = CalculatorBrain(
weight: weightStartValue, height: heightStartValue);
Navigator.pushNamed(
context,
'/first',
arguments: calc,
// arguments: OutputPage(
// getOutput: calc.outputBMI(),
// getNumber: calc.calculateBMI(),
// getInterpretation: calc.interpretationBMI(),
);
},
),
],
),
);
}
}
class BottomButtons extends StatelessWidget {
BottomButtons({this.buttonText, this.onTap});
final String? buttonText;
final Function? onTap;
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
onTap!();
},
child: Container(
padding: EdgeInsets.only(top: 21),
child: Text(
buttonText!,
textAlign: TextAlign.center,
style: TextStyle(
color: Color(0xFFFFFFFF),
fontStyle: FontStyle.italic,
fontWeight: FontWeight.w600,
letterSpacing: 2,
fontSize: 20,
),
),
margin: EdgeInsets.all(10),
height: kBottomBarHeight,
width: double.infinity,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12),
color: kBottomContainerColor,
),
),
);
}
}
class CustomButton extends StatelessWidget {
CustomButton({@required this.iconMarker, this.afterPress});
final IconData? iconMarker;
final Function? afterPress;
@override
Widget build(BuildContext context) {
return RawMaterialButton(
child: Icon(iconMarker),
onPressed: () {
afterPress!();
},
shape: CircleBorder(),
fillColor: Color(0xFF4C4F5E),
highlightColor: Color(0x294C4F5E),
constraints: BoxConstraints.tightFor(width: 56, height: 56),
);
}
}
output_page.dart
import 'package:bmi_calculator/container_cards.dart';
import 'package:bmi_calculator/input_page.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'constants.dart';
import 'package:bmi_calculator/calculator_brain.dart';
class OutputPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
final CalculatorBrain calc =
ModalRoute.of(context)!.settings.arguments as CalculatorBrain;
return Scaffold(
appBar: AppBar(
title: Text(
'BMI CALCULATOR',
),
centerTitle: true,
),
body: Padding(
padding: const EdgeInsets.all(15.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
'Your Parameters',
style: kOutputText,
textAlign: TextAlign.center,
),
Expanded(
flex: 5,
child: ContainerCard(
myColor: kInactiveBoxColors,
myCardDetails: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text(
calc.calculateBMI(),
style: kNumberStyles,
textAlign: TextAlign.center,
),
Text(
calc.resultBMI().toUpperCase(),
style: kResultText,
textAlign: TextAlign.center,
),
Text(
'Normal BMI Range : \n 18.5 - 25 kg/m2',
style: TextStyle(
color: Color(0xFFF9E1B1),
),
textAlign: TextAlign.center,
),
Text(
calc.analysisBMI(),
style: kAnalysisTextStyles,
textAlign: TextAlign.center,
),
],
),
),
),
BottomButtons(
buttonText: 'Re-Calculate',
onTap: () {
Navigator.pushNamed(context, '/');
},
),
],
),
),
);
}
}
calculator_brain.dart
import 'dart:math';
class CalculatorBrain {
final int weight;
final int height;
CalculatorBrain({
required this.weight,
required this.height,
});
double? _bmi;
String calculateBMI() {
_bmi = weight / pow(height / 100, 2);
return _bmi.toStringAsFixed(2);
}
String outputBMI() {
if (_bmi <= 16.0) {
return 'Underweight(Severe thinness)';
} else if (_bmi < 16.9) {
return 'Underweight(Moderate thinness)';
} else if (_bmi < 18.4) {
return 'Underweight(Mild thinness)';
} else if (_bmi <= 25) {
return 'Normal';
} else if (_bmi < 29.9) {
return 'Overweight';
} else if (_bmi < 34.9) {
return 'Obese(Class I)';
} else if (_bmi < 39.9) {
return 'Obese(Class II)';
} else
return '(Obese(Class III)';
}
String analysisBMI() {
if (_bmi <= 16.0) {
return 'Health Risk : \n Possible nutritional deficiency and osteoporosis. This is severe. Please consult your Physician Immediately';
} else if (_bmi < 16.9) {
return 'Health Risk : \n Possible nutritional deficiency and osteoporosis. Please consult your Physician Immediately';
} else if (_bmi < 18.4) {
return 'Health Risk : \n Possible nutritional deficiency and osteoporosis. Your Physician should be consulted';
} else if (_bmi <= 25) {
return 'Congratulations! \n You are living a healthy life. Please keep it up';
} else if (_bmi < 29.9) {
return 'Health Risk : \n Moderate risk of developing heart disease, high blood pressure, stroke, diabetes mellitus. Please take care';
} else if (_bmi < 34.9) {
return 'Health Risk : \n High risk of developing heart disease, high blood pressure, stroke, diabetes mellitus. Metabolic Syndrome';
} else if (_bmi < 39.9) {
return 'Health Risk : \n Very High risk of developing heart disease, high blood pressure, stroke, diabetes mellitus. Metabolic Syndrome. Please consult your Physician';
} else
return 'Health Risk : \n Extremely High risk of developing heart disease, high blood pressure, stroke, diabetes mellitus. Metabolic Syndrome. Physician consultation advised immediately';
}
}
container_cards.dart
import 'package:flutter/material.dart';
class ContainerCard extends StatelessWidget {
ContainerCard({this.myColor, this.myCardDetails, this.onPress});
final Color? myColor;
final Widget? myCardDetails;
final Function? onPress;
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () => {onPress?.call()},
child: Container(
child: myCardDetails,
margin: EdgeInsets.all(10),
decoration: BoxDecoration(
color: myColor,
borderRadius: BorderRadius.all(
Radius.circular(12),
),
),
),
);
}
}
first_line_containers.dart
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:bmi_calculator/constants.dart';
class FirstLineContainers extends StatelessWidget {
FirstLineContainers(
{this.myIcons = const Icon(FontAwesomeIcons.male), this.myStrings});
final Icon myIcons;
final String? myStrings;
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
myIcons.icon,
size: 60.0,
),
SizedBox(height: 15),
Text(myStrings!, style: kTextStyles),
],
);
}
}
constants.dart
import 'package:flutter/material.dart';
const kBottomBarHeight = 80.0;
const kBottomContainerColor = Color(0xFF363A3D);
const kInactiveBoxColors = Color(0xFF1D1F33);
const kActiveBoxColors = Color(0xFF575861);
const kTextStyles = TextStyle(
color: Color(0xFF8D8D98),
fontSize: 22,
);
const kTextColor = TextStyle(
color: Color(0xFF8D8D98),
);
const kNumberStyles = TextStyle(
fontWeight: FontWeight.w800,
fontSize: 50,
);
const kActiveTrackColor = Color(0xFFE4706F);
const kInactiveTrackColor = Color(0xFF8D602A);
const kThumbColor = Color(0xFFEBAB25);
const kOverlayColor = Color(0x29EBAB25);
const kThumbShape = RoundSliderThumbShape(
enabledThumbRadius: 12,
);
const kOverlayShape = RoundSliderOverlayShape(overlayRadius: 25);
const kValueIndicatorShape = PaddleSliderValueIndicatorShape();
const kValueIndicatorColor = Color(0xFFEBAB25);
const kOutputText = TextStyle(
fontWeight: FontWeight.w500,
fontSize: 30,
color: Color(0xFF8E919E),
);
const kResultText = TextStyle(
fontWeight: FontWeight.w600,
fontStyle: FontStyle.normal,
fontSize: 30,
letterSpacing: 3,
color: Color(0xFFF4C18B),
);
const kAnalysisTextStyles = TextStyle(
color: Color(0xFF8D8D98),
fontSize: 16,
);
The following errors are thrown:
Exception caught by widgets library
The following TypeErrorImpl was thrown building OutputPage(dirty, dependencies: [_ModalScopeStatus]):
Unexpected null value.
The relevant error-causing widget was:
OutputPage file:///C:/Users/Debasis/StudioProjects/bmi_calculator/lib/main.dart:33:32
When the exception was thrown, this was the stack:
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 236:49 throw_
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 518:63 nullCheck
packages/bmi_calculator/calculator_brain.dart 20:15 outputBMI
packages/bmi_calculator/output_page.dart 51:27 build
packages/flutter/src/widgets/framework.dart 4648:28 build
Can you please help ?
Upvotes: 3
Views: 9624
Reputation: 631
The solution to my Problem :
In the calculator_brain.dart I needed to put in double _bmi = 0; instead of double? _bmi;
calculator_brain.dart
import 'dart:math';
class CalculatorBrain {
CalculatorBrain({
required this.weight,
required this.height,
});
final int weight;
final int height;
double _bmi = 0;
String calculateBMI() {
_bmi = weight / pow(height / 100, 2);
return _bmi.toStringAsFixed(2);
}
String resultBMI() {
if (_bmi <= 16.0) {
return 'Underweight\n(Severe thinness)';
} else if (_bmi < 16.9) {
return 'Underweight\n(Moderate thinness)';
} else if (_bmi < 18.4) {
return 'Underweight\n(Mild thinness)';
} else if (_bmi <= 25) {
return 'Normal';
} else if (_bmi < 29.9) {
return 'Overweight';
} else if (_bmi < 34.9) {
return 'Obese\n(Class I)';
} else if (_bmi < 39.9) {
return 'Obese\n(Class II)';
} else
return '(Obese\n(Class III)';
}
String analysisBMI() {
if (_bmi <= 16.0) {
return 'Health Risk : \n Possible nutritional deficiency and osteoporosis. This is severe. Please consult your Physician Immediately';
} else if (_bmi < 16.9) {
return 'Health Risk : \n Possible nutritional deficiency and osteoporosis. Please consult your Physician Immediately';
} else if (_bmi < 18.4) {
return 'Health Risk : \n Possible nutritional deficiency and osteoporosis. Your Physician should be consulted';
} else if (_bmi <= 25) {
return 'Congratulations! \n You are living a healthy life. Please keep it up';
} else if (_bmi < 29.9) {
return 'Health Risk : \n Moderate risk of developing heart disease, high blood pressure, stroke, diabetes mellitus. Please take care';
} else if (_bmi < 34.9) {
return 'Health Risk : \n High risk of developing heart disease, high blood pressure, stroke, diabetes mellitus. Metabolic Syndrome';
} else if (_bmi < 39.9) {
return 'Health Risk : \n Very High risk of developing heart disease, high blood pressure, stroke, diabetes mellitus. Metabolic Syndrome. Please consult your Physician';
} else
return 'Health Risk : \n Extremely High risk of developing heart disease, high blood pressure, stroke, diabetes mellitus. Metabolic Syndrome. Physician consultation advised immediately';
}
}
Upvotes: 2