【问题标题】:The following NoSuchMethodError was thrown building Builder(dirty): The method '>=' was called on null. Receiver: null The relevant error-causing was:在构建 Builder(dirty) 时引发了以下 NoSuchMethodError:在 null 上调用了方法 '>='。 Receiver: null 相关的错误原因是:
【发布时间】:2021-05-09 07:17:31
【问题描述】:

所以,我想通过调用方法并将数据传递到字符串中,将数据传递到新屏幕。第一个方法 calc.calculateBMI() 成功传入 bmiResult.. 但是我得到了下面的错误 calc.getInterpretation

第一个屏幕的代码。

ButtomButton(
            buttonTitle: 'CALCULATE',
            onTap: (){

              CalculatorBrain calc = CalculatorBrain(height: height, weight: weight);

              Navigator.push(context, MaterialPageRoute(builder: (context){
                return ResultsPage(
                  bmiResult: calc.calculateBMI(),
                  interpretation: calc.getInterpretation(),
                );
              }));
            },
          ),
import 'dart:math';

class CalculatorBrain {
  CalculatorBrain({this.height, this.weight});

  final int height;
  final int weight;

  double _bmi;

  String calculateBMI() {
    double _bmi = weight / pow(height/100, 2);
    return _bmi.toStringAsFixed(1);
  }

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';
    }
  }
}

我遇到的错误

======== Exception caught by widgets library =======================================================
The following NoSuchMethodError was thrown building Builder(dirty):
The method '>=' was called on null.
Receiver: null
Tried calling: >=(27)

The relevant error-causing widget was: 
  MaterialApp file:///C:/Users/MICHEAL/AndroidStudioProjects/bmi_calculator/lib/main.dart:9:12
When the exception was thrown, this was the stack: 
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
#1      CalculatorBrain.getInterpretation (package:bmi_calculator/calculator_brain.dart:27:14)
#2      _InputPageState.build.<anonymous closure>.<anonymous closure> (package:bmi_calculator/screens/input_page.dart:214:40)
#3      MaterialPageRoute.buildContent (package:flutter/src/material/page.dart:55:55)
#4      MaterialRouteTransitionMixin.buildPage (package:flutter/src/material/page.dart:108:27)
...
====================================================================================================

【问题讨论】:

标签: flutter dart


【解决方案1】:

上面代码中的错误是因为我们没有在CalculatorBrain类中初始化_bmi变量。

为此,我们可以使用以下代码继续:

import 'dart:math';

class CalculatorBrain {
  CalculatorBrain({this.height, this.weight}) {
    _bmi = weight / pow(height/100, 2);
  }

  final int height;
  final int weight;

  double _bmi;

  String calculateBMI() =>
    _bmi.toStringAsFixed(1);

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';
    }
  }
}

具有 null-safety 的相同 sn-p 将是:

import 'dart:math';

class CalculatorBrain {
  CalculatorBrain({required this.height, required this.weight}) {
    _bmi = weight / pow(height / 100, 2);
  }

  final int height;
  final int weight;

  late double _bmi;

  String calculateBMI() => _bmi.toStringAsFixed(1);

  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';
    }
  }
}

【讨论】:

  • 如果你在构造函数中计算它,为什么它可以为空?它可能是最终不可为空的。
  • 感谢您的评论@nvoigt!我已经编辑了答案,改进了空安全版本。 _bmi 不应该是 final,而是 late,因为我们在类构造函数中计算它。
猜你喜欢
  • 2022-08-11
  • 1970-01-01
  • 2020-10-06
  • 1970-01-01
  • 2020-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-23
相关资源
最近更新 更多