flutter

How to go with this error "The method '/' was called on null"


I was just doing a BMI Calculator with Flutter but end up having this error but when I look at all my codes I could not find the error.

Here is my calculator_brain.dart file where reside all my conditions:

import 'dart:math';

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

  String getInterpretation() {
    if (_bmi >= 25) {
      return 'You have a higher than the normal body weight! You need to exercice more...';
    } else if (_bmi >= 18.5) {
      return 'You\'ve got a normal body weight! Good Job!';
    } else {
      return 'you have a lower than the normal body weight! You\'d be eating more...';
    }
  }
}

my results.dart to display everything. it is even the screen which is not showing for the error

import 'package:bmi_calculator/constants.dart';
import 'package:bmi_calculator/components/reusable_card.dart';
import 'package:flutter/material.dart';
import '../components/bottom_button.dart';

class Results extends StatelessWidget {
  Results(
      {@required this.bmiResult,
      @required this.resultText,
      @required this.interpretation});
  final String bmiResult;
  final String resultText;
  final String interpretation;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('BMI CALCULATOR'),
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: <Widget>[
          Expanded(
            child: Container(
              padding: EdgeInsets.all(15.0),
              alignment: Alignment.bottomLeft,
              child: Text(
                'Your Result',
                style: kTitleTextStyle,
              ),
            ),
          ),
          Expanded(
            flex: 5,
            child: ReusableCard(
              colour: kActiveCardColor,
              cardChild: Column(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  Text(
                    resultText.toUpperCase(),
                    style: kResultTextStyle,
                  ),
                  Text(
                    bmiResult,
                    style: kBMITextstyle,
                  ),
                  Text(
                    interpretation,
                    style: kBodyTextStyle,
                    textAlign: TextAlign.center,
                  ),
                ],
              ),
            ),
          ),
          Expanded(
            child: BottomButton(
              buttonTitle: 'RE-CALCULATE',
              onTap: () {
                Navigator.pop(context);
              },
            ),
          )
        ],
      ),
    );
  }
}

Lastly, the object that I'm creating for the calculator and the navigator:

  BottomButton(
        buttonTitle: 'CALCULATE',
        onTap: () {
          CalculatorBrain calculationBrain = CalculatorBrain();
          Navigator.push(
            context,
            MaterialPageRoute(
              builder: (context) {
                return Results(
                  bmiResult: calculationBrain.calculateBMI(),
                  resultText: calculationBrain.getResult(),
                  interpretation: calculationBrain.getInterpretation(),
                );
              },
            ),
          );
        },
      ),

The complete error I am receiving is "The following NoSuchMethodError was thrown building Builder(dirty)":

The method '/' was called on null.
Receiver: null
Tried calling: /(100)

The relevant error-causing widget was: 
  MaterialApp file:///Users/macair13/AndroidStudioProjects/bmi-calculator-flutter/lib/main.dart:9:12
When the exception was thrown, this was the stack: 
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)
#1      CalculatorBrain.calculateBMI (package:bmi_calculator/calculator_brain.dart:11:32)
#2      _InputPageState.build.<anonymous closure>.<anonymous closure> (package:bmi_calculator/screens/input_page.dart:222:51)
#3      MaterialPageRoute.buildPage (package:flutter/src/material/page.dart:87:27)
#4      _ModalScopeState.build.<anonymous closure> (package:flutter/src/widgets/routes.dart:710:43)

Solution

  • From the code, I see where are you passing your data to be calculated. I mean from where you are passing the height and weight, it cannot calculate because it is not getting the values. An when you press the bottom button it is not getting anything so null cannot be / so it's giving the error.

    CalculatorBrain calculationBrain = CalculatorBrain();
    

    This is the code that you have written and if you check it using this

    CalculatorBrain calculationBrain = CalculatorBrain(weight: 5,height: 5);
    

    passing the value in constructor might solve the issue.

    Just check and let me know if it works.