【发布时间】:2020-06-12 18:04:21
【问题描述】:
我只是用 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)
【问题讨论】:
-
您正尝试在除法运算符中使用 null。我们需要源代码的行号才能解释异常。
-
@GazihanAlankus 如何添加我的源代码的这些行
-
这个答案对你有用吗?
标签: flutter