How to store a return value in variable flutter

Issue

I need a little help.

I have this function in another file and i want to store the return value in a variable, because i dont want to repeat the same code again and again and I want to reuse it as many times I want.

here is the code in another file.

double dropDownIf(dropDownVal, finalVal, valParsed) {

  if(dropDownVal == 'm'){
        finalVal = valParsed;
      } else if(dropDownVal == 'cm'){
        finalVal = valParsed/100;
      } else if(dropDownVal == 'mm'){
        finalVal = valParsed/1000;
      }
      print('here is the updated value $finalVal');
   return finalVal;
}

as you can see that it show the return value in debug console but it doesnt show the value in another page in which i am using this code.

here is the code on another page.

dropDownIf(dropDownValueL, finalLength, lengthParsed);
      
      print(finalLength);

here in this page, the print function shows 0, I have declared the double finalLength = 0; in the beginning of the file. so the print shows 0 instead of the updated value.

the middle value in the dropDownIf function is the return value but it doesnt work.

Solution

You need to store the return value of the method in a variable, then use it:

double returnValue = dropDownIf(dropDownValueL, finalLength, lengthParsed);
 print(returnValue);

Answered By – Gwhyyy

Answer Checked By – Timothy Miller (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *