How to store a return value in variable flutter - 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.
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.

You need to store the return value of the method in a variable, then use it:
double returnValue = dropDownIf(dropDownValueL, finalLength, lengthParsed);
print(returnValue);

Related

I can't use a variable outside my loop (flutter)

I have a problem with my flutter app, I'm trying to use a variable to stock the name of a market, but when I use this variable, outside my loop, it showed nothing, I don't know why
var markets = [
["Marjane", "marjane-offers", 34.0590237712, -6.80169582367],
["Auto Hall", "autohall-offers", 34.0636, -6.7973],
["Bricoma", "bricoma-offers", 34.0706, -6.7883],
];
for (var i = 0; i < markets.length; i++) {
// GEOLOCATOR PACK
double stopLatitude = double.parse("${markets[i][2]}");
double stopLongitude = double.parse("${markets[i][3]}");
double distanceInMeters = Geolocator.distanceBetween(startLatitude, startLongitude, stopLatitude, stopLongitude);
if (distanceInMeters < 1000) {
var nameMarket = markets[i][0];
}
}
return Text('Location always change: \n${data.latitude}/${data.longitude} \n\n A9RAB MAHAL LIK: $nameMarket');
(This code checks if I'm close to a store and show to me his name!)
I assume you are referring to the variable nameMarket. At the point of the return statement, the variable would not be in scope. What you can do is declare the variable like String? nameMarket before the for loop (which would be in scope of the return statement). And then assign to it like nameMarket = markets[i][0];
Actually, better yet might be to move the return statement into the if statement if you just want the first store that is close enough without checking the remaining stores.

use a for loop to add element in to the java.util.ArrayList,but the list's address of the reference always changing

I have a strange problem when I use a for loop to add element in to the java.util.ArrayList , but the list's address of the reference always changing
Here is the code:
var curntRow: Row = null
var startTime: lang.Long = null
//this is the list
var standTime: util.ArrayList[Row] = new util.ArrayList[Row]()
for (row <- usersCoorOrderByTime) {
if (curntRow == null) {
startTime = row.getAs[lang.Long](2)
} else if (!row.getAs[String](1).equals(curntRow.getAs[String](1))) {
//And I use the method list.add() right here
standTime.add(Row(row.getAs[String](0), row.getAs[String](1), row.getAs[DoubleType](4), row.getAs[DoubleType](5), curntRow.getAs[lang.Long](2) - startTime))
startTime = row.getAs[lang.Long](2)
}
curntRow = row
}
And please see the pic that I debug below:
addr is "7703"
Before get in the loop The list's addr is "7703"
When is get in the loop ,the address changes
change to "11268"
change to "11287"
The most strange things is when it end the loop, the address has changed back to where it was originally declared
change back to "7703"
finally I get an empty ArrayList
I found the error
the parameter of for loop is Dataframe, I should turn to Array or List then make for loop
Try using Mutable ArrayBuffer. Below is a simple example. I hope it helps.
val x = scala.collection.mutable.ArrayBuffer[String]()
x += "2"
x += "4"
println(x)

How to get and set Real value from RealEdit in a Form's Grid ?

I have in a Grid a RealEdit , I set the autodeclaration YES.
The name is myRealEdit , DataSource is myTable and the Field is myRealField.
In the modified method I want to get the value, I need to do a IF control.
IF the value is 0 change the Filed's value IF the value is not 0
throws the value entered and restores the previous value.
I used this code, in modified method:
public boolean modified()
{
boolean ret;
real storedValue;
ret = super();
storedValue = myTable.myRealField; // there is another way to get the value ?
if (myRealEdit.valueStr() == "0")
//accept the value
if (!myRealEdit.valueStr() != "0")
{
myRealEdit.realValue(storedValue);
}
return ret;
}
If the value is not 0 (zero) don't restore the previous value.
I have to use another method ? There is another way to get the real value ?
Thanks in advice,
enjoy!!
Since you are using the modified method in your answer, I suppose you want to put this field validation on the control level (instead of the datasource or table level).
As #Jan B. Kjeldsen suggested in his comment, you should use the validate method to do this validation. Use the modified method only if you want to add some logic that is executed in addition to the field value modification.
The validate method could look similar to
public boolean validate()
{
return this.realValue() == 0 && super() || checkFailed(strFmt("Value %1 is not permitted", this.realValue()));
// TODO please replace this with a Label and explain to the user why the value is not permitted and what he or she can do to resolve this
}
I find a possible way,
I used this code:
public boolean modified()
{
boolean ret;
if (myRealEdit.valueStr() == "0")
{
//accept the value
ret = super();
}
if (!myRealEdit.valueStr() != "0")
{
info("Value not permit");
// nothing to do
}
return ret;
}
In this way, if and only if I have a value 0 I modified the value.
I need to get or read the Real value just inserted from myRealEdit in the modified method.
If the community has comments or improvements inserted, will be more information.

Filter getElementsByTagName list by option values

I'm using getElementsByTagName to return all the select lists on a page - is it possible to then filter these based upon an option value, ie of the first or second item in the list?
The reason is that for reasons I won't go into here there are a block of select lists with number values (1,2,3,4,5 etc) and others which have text values (Blue and Black, Red and Black etc) and I only want the scripting I have to run on the ones with numerical values. I can't add a class to them which would more easily let me do this however I can be certain that the first option value in the list will be "1".
Therefore is there a way to filter the returned list of selects on the page by only those whose first option value is "1"?
I am pretty sure that there is a better solution, but for the moment you can try something like:
var allSelect = document.getElementsByTagName("select");
var result = filterBy(allSelect, 0/*0 == The first option*/, "1"/* 1 == the value of the first option*/);
function filterBy(allSelect, index, theValue) {
var result = [];
for (var i = 0; i < allSelect.length; i++) {
if(allSelect[i].options[index].value == theValue ) {
result.push(allSelect[i]);
}
}
return result;
}
I managed to get this working by wrapping a simple IF statement around the action to be performed (in this case, disabling options) as follows:
inputs = document.getElementsByTagName('select');
for (i = 0; i < inputs.length; i++) {
if (inputs[i].options[1].text == 1) {
// perform action required
}
}
No doubt there is a slicker or more economic way to do this but the main thing is it works for me.

do something when counter = x

I have a counter that make counter++ every time one image touches another image.
Now what I want to do is: if counter=2; do something, but I always get an error:
Assignment makes pointer from integer without a cast
Here is a part of the code:
-(void)checkcollision {
if(CGRectIntersectsRect(flakeImage.frame, viewToRotate.frame)) {
counter++;
}
}
-(void)checknumber {
if(counter=2) {
viewToRotate.alpha=0;
}
}
Are you perhaps doing this:
if (counter = 2) {
// Do something.
}
This is a common error in if statements. The correction would be:
if (counter == 2) { // Note the "==", instead of "="
// Do something.
}
This is just a guess though - I would need to see some more information about the error, or about what you want to do.
EDIT
Ah - have seen your newly posted code, confirming what I stated above. Your code reads that you are trying to assign the value '2' to counter in the if statement. You want the == to make this a check for equality.