Home > Software engineering >  Flutter double paint value calculation errors
Flutter double paint value calculation errors

Time:01-25

Getting this error:

The setter 'value=' was called on null. Receiver: null Tried calling: value=3000.0

final WallheightController = TextEditingController();
final WalllengthController = TextEditingController();
var height;
var length;
var area;
RxDouble paint = 0.2.obs;
RxDouble result=0.0 .obs;
calculatepaint() {
  if (WallheightController.text.isNotEmpty ||
      WalllengthController.text.isNotEmpty ) {
     height = double.parse(WallheightController.text);
     length = double.parse(WalllengthController.text);

    area.value = (height * length);
    result.value = area.value* paint.value;

    Get.toNamed(RoutesClass.prcalculation);
   } else {
    Get.snackbar("Fields empty", "Fill Required fields");
  }
}

clear() {
  WallheightController.clear();
  WalllengthController.clear();
}

I am trying to calculate paint on an area of a wall but getting this error

CodePudding user response:

make var area = 0.0.obs;

you are using area.value so you need to make area .obs

CodePudding user response:

I have gone through your and I have noticed something that I want to share with you.

  1. You should always try to use variables with proper types like double, int, String and try to omit the var keyword as much as possible.

  2. When you are using GetX for your State Management solution, you can either make the variable reactive by .obs or you can set your variable as it. But whenever you want to reflect the changes in the UI you can call the update() method at the end of your task or function.

Therefore there are mainly 2 answers to your question:

  1. You can make the variables reactive by .obs like this:
final WallheightController = TextEditingController();
final WalllengthController = TextEditingController();
late double height; // I have used late because we are not assigning the value when declaring the variable
late double length;
RxDouble area = 0.0.obs;
RxDouble paint = 0.2.obs;
RxDouble result = 0.0.obs;
calculatepaint() {
  if (WallheightController.text.isNotEmpty ||
      WalllengthController.text.isNotEmpty ) {

    height = double.parse(WallheightController.text);
    length = double.parse(WalllengthController.text);

    area.value = (height * length);
    result.value = area.value* paint.value;

    Get.toNamed(RoutesClass.prcalculation);

   } else {
    Get.snackbar("Fields empty", "Fill Required fields");
  }
}
  1. You can use update() method:
final WallheightController = TextEditingController();
final WalllengthController = TextEditingController();
late double height; // I have used late because we are not assigning the value when declaring the variable
late double length;
double area = 0.0;
double paint = 0.2;
double result = 0.0;
calculatepaint() {
  if (WallheightController.text.isNotEmpty ||
      WalllengthController.text.isNotEmpty ) {

    height = double.parse(WallheightController.text);
    length = double.parse(WalllengthController.text);

    area = (height * length);
    result = area* paint;
    update();

    Get.toNamed(RoutesClass.prcalculation);

   } else {
    update();
    Get.snackbar("Fields empty", "Fill Required fields");
  }
}

If you are using second method, then don't forget to Wrap your code with GetBuilder instead of Obx.

  • Related