Home > Software design >  Flutter double value calculation errors
Flutter double value calculation errors

Time:01-17

I am a Beginner and I want to calculate 4 values and get area but I keep getting different errors my code is not good so please give advices Like: Class 'String' has no instance method '/'. Receiver: "56" Tried calling: /(2) I am taking values from text fields I want them to be calculated in double.

This is My Code ` class SideController extends GetxController{

  final SideAController = TextEditingController();
  final SideBController = TextEditingController();
  final SideCController = TextEditingController();    
  final SideDController = TextEditingController();
  var Result=''.obs;
  var sidea;
  var sideb;
  var sidec;
  var sided;
  var length;
  var width;
  var area = 0.obs;

  calculateheight() {
    if (SideAController.text != null ||
        SideBController.text != null ||
        SideCController.text != null ||
        SideDController.text != null) {
      sidea = SideAController.text;
      sideb = SideBController.text;
      sidec = SideCController.text;
      sided = SideDController.text;

      length = ((sidea)   (sidec)) / 2;
      width = ((sideb)   (sided)) / 2;

      area = (length * width);
      area.value == Result;
      return area;
    } else {
      Get.snackbar("Fields Empty", "Fill all Fields");
    }
  }
}

`

I tried to calculate 4 values and show my result is string but keep getting errors

CodePudding user response:

String values cannot be added or divided. You have to convert the String values to double first.

 sidea = double.parse(SideAController.text);
 sideb = double.parse(SideBController.text);
 sidec = double.parse(SideCController.text);
 sided = double.parse(SideDController.text);

 length = ((sidea)   (sidec)) / 2;
 width = ((sideb)   (sided)) / 2;

 area.value = (length * width);
 // area.value == Result;
 return area;

If you want to return area as String, use return area.toString();

CodePudding user response:

I think there are already variables here. I write down what I already know. For example, there is Result=''.obs;

class SideController extends GetxController {
  final SideAController = TextEditingController();
  final SideBController = TextEditingController();
  final SideCController = TextEditingController();
  final SideDController = TextEditingController();

  var sidea;
  var sideb;
  var sidec;
  var sided;
  var length;
  var width;
  RxDouble area = 0.0.obs;

  dynamic calculateheight() {
    if (SideAController.text.isNotEmpty &&
        SideBController.text.isNotEmpty &&
        SideCController.text.isNotEmpty &&
        SideDController.text.isNotEmpty) {
      sidea = double.parse(SideAController.text);
      sideb = double.parse(SideBController.text);
      sidec = double.parse(SideCController.text);
      sided = double.parse(SideDController.text);
      length = (sidea   sidec) / 2;
      width = (sideb   sided) / 2;
      area.value = length * width;
      return area.value.toString();
    } else {
      Get.snackbar("Fields Empty", "Fill all Fields");
      return ' error';
    }
  }
}

I checked the code I posted, the code I posted should work for you. Optionally, calculateheight() function can return String, double, etc.

  • Related