0

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

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

  • First, please use a current version of flutter, this error should not happen with null-safety. – nvoigt May 09 '21 at 07:31
  • 1
    Does this answer your question? [What is a NoSuchMethod error and how do I fix it?](https://stackoverflow.com/questions/64049102/what-is-a-nosuchmethod-error-and-how-do-i-fix-it) – nvoigt May 09 '21 at 07:31

1 Answers1

1

The error in the code above is caused by the fact that we're not initializing the _bmi variable inside the CalculatorBrain class.

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';
    }
  }
}
Stefano Amorelli
  • 4,553
  • 3
  • 14
  • 30
  • 1
    If you calculate it in the constructor, why have it nullable? It could be final non-nullable. – nvoigt May 09 '21 at 08:02
  • Thanks for your comment @nvoigt! I've edited the answer, improving the null-safety version. `_bmi` shouldn't be `final`, but `late`, since we're calculating it in the class constructor. – Stefano Amorelli May 09 '21 at 08:14