Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to store a return value in variable flutter

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading