简体   繁体   English

在构建 Builder(dirty) 时引发了以下 NoSuchMethodError:在 null 上调用了方法“>=”。 接收器:null 相关的错误原因是:

[英]The following NoSuchMethodError was thrown building Builder(dirty): The method '>=' was called on null. Receiver: null The relevant error-causing was:

So, I want to pass data to a new screen, by calling methods and passing it into a String.所以,我想通过调用方法并将数据传递到一个字符串中,将数据传递到一个新屏幕。 The first method calc.calculateBMI() was passed in successfully into bmiResult.. But I got the error below for calc.getInterpretation第一个方法 calc.calculateBMI() 已成功传递到 bmiResult.. 但是我得到了下面的错误 calc.getInterpretation

First Screen's Code.第一个屏幕的代码。

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

The Error I got我得到的错误

======== 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)
...
====================================================================================================

The error in the code above is caused by the fact that we're not initializing the _bmi variable inside the CalculatorBrain class.上面代码中的错误是由于我们没有初始化CalculatorBrain class 中的_bmi变量。

To do so we can proceed by using the following code:为此,我们可以使用以下代码继续:

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

The same snippet with null-safety would be:具有空安全性的相同代码段将是:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在构建 Builder(dirty) 时引发了以下 NoSuchMethodError:在 null 上调用了 getter 'data'。 接收方:null 尝试调用:数据 - The following NoSuchMethodError was thrown building Builder(dirty): The getter 'data' was called on null. Receiver: null Tried calling: data 在 null 上调用了方法“&gt;”。 接收方:null 尝试调用:&gt;(1e-10) 相关的导致错误的小部件是 Column - The method '>' was called on null. Receiver: null Tried calling: >(1e-10) The relevant error-causing widget was Column 在构建 Home() 时引发了以下 NoSuchMethodError:在 null 上调用了方法“&gt;”。 接收方:null 尝试呼叫:&gt;(1) - The following NoSuchMethodError was thrown building Home(): The method '>' was called on null. Receiver: null Tried calling: >(1) 我该如何解决这个问题? 构建时引发了以下 NoSuchMethodError:在 null 上调用了方法“[]”。 接收方:null 尝试调用:[](0) - How do i fix this issue? The following NoSuchMethodError was thrown building: The method '[]' was called on null. Receiver: null Tried calling: [](0) 在构建 Builder 时抛出了以下 NoSuchMethodError:getter &#39;email&#39; 在 null 上被调用。 在火力基地 - The following NoSuchMethodError was thrown building Builder: The getter 'email' was called on null. in firebase 在构建 MessageBubble(dirty) 时引发了以下 NoSuchMethodError:在 null 上调用了 getter 'millisecondsSinceEpoch' - The following NoSuchMethodError was thrown building MessageBubble(dirty): The getter 'millisecondsSinceEpoch' was called on null 在 null 上调用了 getter 'length'。 接收方:null 尝试调用:长度。 相关的导致错误的小部件是:/bottom_bar.dart:17:64 - The getter 'length' was called on null. Receiver: null Tried calling: length. The relevant error-causing widget was: /bottom_bar.dart:17:64 错误:在 null 上调用了方法“round”。 接收方:null 尝试调用:round()。 构建小部件时抛出错误 - Error: The method 'round' was called on null. Receiver: null Tried calling: round(). Error thrown when building widget 运行时抛出 NoSuchMethodError。 在 null 上调用了方法“[]”。 接收器:null。 尝试调用:[](0)。 如何解决这个错误? - NoSuchMethodError was thrown while running. The method '[]' was called on null. Receiver: null. Tried calling: [](0). How to solve this error? (以下 NoSuchMethodError 在构建 Builder 时被抛出:)在 null 上调用了 setter 'categoryName=' - (The following NoSuchMethodError was thrown building Builder:) The setter 'categoryName=' was called on null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM