簡體   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:

所以,我想通過調用方法並將數據傳遞到一個字符串中,將數據傳遞到一個新屏幕。 第一個方法 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)
...
====================================================================================================

上面代碼中的錯誤是由於我們沒有初始化CalculatorBrain class 中的_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';
    }
  }
}

具有空安全性的相同代碼段將是:

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 嘗試調用:數據 在 null 上調用了方法“&gt;”。 接收方:null 嘗試調用:&gt;(1e-10) 相關的導致錯誤的小部件是 Column 在構建 Home() 時引發了以下 NoSuchMethodError:在 null 上調用了方法“&gt;”。 接收方:null 嘗試呼叫:&gt;(1) 我該如何解決這個問題? 構建時引發了以下 NoSuchMethodError:在 null 上調用了方法“[]”。 接收方:null 嘗試調用:[](0) 在構建 Builder 時拋出了以下 NoSuchMethodError:getter &#39;email&#39; 在 null 上被調用。 在火力基地 在構建 MessageBubble(dirty) 時引發了以下 NoSuchMethodError:在 null 上調用了 getter 'millisecondsSinceEpoch' 在 null 上調用了 getter 'length'。 接收方:null 嘗試調用:長度。 相關的導致錯誤的小部件是:/bottom_bar.dart:17:64 錯誤:在 null 上調用了方法“round”。 接收方:null 嘗試調用:round()。 構建小部件時拋出錯誤 運行時拋出 NoSuchMethodError。 在 null 上調用了方法“[]”。 接收器:null。 嘗試調用:[](0)。 如何解決這個錯誤? (以下 NoSuchMethodError 在構建 Builder 時被拋出:)在 null 上調用了 setter 'categoryName='
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM