简体   繁体   中英

NoSuchMethodError, Exception caught by widgets library

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

The relevant error-causing widget was: MaterialApp file:///C:/Users/Ahmed/AndroidStudioProjects/bmi_calc/lib/main.dart:8:12 When the exception was thrown, this was the stack:

0 Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)

1 calculator.getresult (package:bmicalc/calculator.dart:14:14)

2 _InputPageState.build.. (package:bmicalc/input_page.dart:226:97)

3 MaterialPageRoute.buildPage (package:flutter/src/material/page.dart:87:27)

4 _ModalScopeState.build. (package:flutter/src/widgets/routes.dart:710:43)

At #1 my code is

class calculator {
  calculator({this.height, this.weight});
  final int height;
  final 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';
    }
    return ' ';
  }

  String getRemarks() {
    if (_bmi >= 25) {
      return 'Your weight is more than average body weight, try to excercise.';
    } else if (_bmi > 18.5) {
      return 'Your weight is normal';
    } else {
      return 'Your weight is less than average body weight, try to eat more';
    }
  }
}

At #2

`GestureDetector(  
onTap: () {
calculator cal = calculator(height: height, weight: weight);
Navigator.push(context, MaterialPageRoute(builder: (context) => results(result: calculator().getresult(), bmi: calculator().calculatebmi(), remarks: calculator().getRemarks())));
},)`

The problem occurs because the value _bmi is null. And it is because you are not calling calculatebmi().

You are calculating BMI with empty parameters of calculator(). So, instead of setting

bmi:calculator().getresult();

use as:

bmi : cal.getresult();         // You already have insteance of calculator (cal)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM