【问题标题】:How to go with this error "The method '/' was called on null"如何解决此错误“方法'/'在null上被调用”
【发布时间】: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


【解决方案1】:

从代码中,我知道您将要计算的数据传递到哪里。我的意思是从你传递身高和体重的地方,它无法计算,因为它没有得到值。当您按下底部按钮时,它没有得到任何东西,所以 null 不能是 / 所以它给出了错误。

CalculatorBrain calculationBrain = CalculatorBrain();

这是您编写的代码,如果您使用它检查它

CalculatorBrain calculationBrain = CalculatorBrain(weight: 5,height: 5);

在构造函数中传递值可能会解决问题。

只要检查一下,让我知道它是否有效。

【讨论】:

    【解决方案2】:

    在BottomButton 小部件中,它应该是ResultsPage 而不是Result?这可能会导致数据未传递到 ResultsPage 类。

    In the BottomButton widget, should it be ResultsPage instead of Result? 
    
    builder: (context) {
                return ResultsPage(
                  bmiResult: calculationBrain.calculateBMI(),
                  resultText: calculationBrain.getResult(),
                  interpretation: calculationBrain.getInterpretation(),
                );
              },
    

    【讨论】:

      猜你喜欢
      • 2021-08-06
      • 2021-02-05
      • 2021-08-14
      • 2021-12-17
      • 1970-01-01
      • 2020-09-12
      • 2020-05-06
      • 2019-11-05
      • 1970-01-01
      相关资源
      最近更新 更多