我只是用 Flutter 做一个 BMI计算器,但最终出现此错误,但是当我查看所有代码时,找不到该错误。
这是我的calculator_brain.dart
文件,该文件驻留了我所有的条件:
import 'dart:math';
class CalculatorBrain {
CalculatorBrain({this.height, this.weight});
int height;
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 the normal body weight! You need to exercice more...';
} else if (_bmi >= 18.5) {
return 'You\'ve got a normal body weight! Good Job!';
} else {
return 'you have a lower than the normal body weight! You\'d be eating more...';
}
}
}
我的results.dart显示所有内容。甚至没有显示错误的屏幕
import 'package:bmi_calculator/constants.dart';
import 'package:bmi_calculator/components/reusable_card.dart';
import 'package:flutter/material.dart';
import '../components/bottom_button.dart';
class Results extends StatelessWidget {
Results(
{@required this.bmiResult,
@required this.resultText,
@required this.interpretation});
final String bmiResult;
final String resultText;
final String interpretation;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('BMI CALCULATOR'),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Expanded(
child: Container(
padding: EdgeInsets.all(15.0),
alignment: Alignment.bottomLeft,
child: Text(
'Your Result',
style: kTitleTextStyle,
),
),
),
Expanded(
flex: 5,
child: ReusableCard(
colour: kActiveCardColor,
cardChild: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text(
resultText.toUpperCase(),
style: kResultTextStyle,
),
Text(
bmiResult,
style: kBMITextstyle,
),
Text(
interpretation,
style: kBodyTextStyle,
textAlign: TextAlign.center,
),
],
),
),
),
Expanded(
child: BottomButton(
buttonTitle: 'RE-CALCULATE',
onTap: () {
Navigator.pop(context);
},
),
)
],
),
);
}
}
最后,我正在为计算器和导航器创建的对象:
BottomButton(
buttonTitle: 'CALCULATE',
onTap: () {
CalculatorBrain calculationBrain = CalculatorBrain();
Navigator.push(
context,
MaterialPageRoute(
builder: (context) {
return Results(
bmiResult: calculationBrain.calculateBMI(),
resultText: calculationBrain.getResult(),
interpretation: calculationBrain.getInterpretation(),
);
},
),
);
},
),
我收到的完整错误是"The following NoSuchMethodError was thrown building Builder(dirty)"
:
The method '/' was called on null.
Receiver: null
Tried calling: /(100)
The relevant error-causing widget was:
MaterialApp file:///Users/macair13/AndroidStudioProjects/bmi-calculator-flutter/lib/main.dart:9:12
When the exception was thrown, this was the stack:
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)
#1 CalculatorBrain.calculateBMI (package:bmi_calculator/calculator_brain.dart:11:32)
#2 _InputPageState.build.<anonymous closure>.<anonymous closure> (package:bmi_calculator/screens/input_page.dart:222:51)
#3 MaterialPageRoute.buildPage (package:flutter/src/material/page.dart:87:27)
#4 _ModalScopeState.build.<anonymous closure> (package:flutter/src/widgets/routes.dart:710:43)
答案 0 :(得分:2)
从代码中,我看到您要将数据传递到哪里。我的意思是从那里传递身高和体重的地方,它无法计算,因为它没有得到值。当您按下底部按钮时,它什么也没得到,因此null不能为/,所以会给出错误。
CalculatorBrain calculationBrain = CalculatorBrain();
这是您编写的代码,如果使用此代码进行检查
CalculatorBrain calculationBrain = CalculatorBrain(weight: 5,height: 5);
在构造函数中传递值可能会解决问题。
只需检查一下,让我知道它是否有效。
答案 1 :(得分:0)
在BottomButton小部件中,它应该是ResultsPage而不是Result吗?这可能导致数据无法传递到ResultsPage类。
processor