Letās do some debugging and start with the basics.
We want to be sure that the app variable name actually contains a list so add a list viewer to your screen and add a button and put this code
Iāve added label updates that indicate progress. These are essential when debugging JSON objects.
The āfor each itemā loop eliminate the need for the āin listā¦get itemā block, so it make the code a little easier to read. If you end up nesting loops, it really makes a difference in readability
Iāve created a silly āDistance Calculationā function. It is NOT EVEN CLOSE to correct. But the advantage is that you can create this function and pretend it returns the correct distance. Assuming the distance is correct, you can build out the rest of the function and display. Then you can go back and put it a better distance calculation later and the rest of the code will continue to work without any further modifications (encapsulation at work). See my previous post in this thread for a link to the haversine function discussion.
The latitude and longitude you have stored in firebase are strings, as indicated by the quotation marks. You need to convert these to number by adding zero when performing comparisons.
The APP variable ClosestCustomer contains the entire object after the loop has completed, so you can access all of the properties using that object without worrying about its position in the list.
That variable saves the closest customer to the app user. Notice that the variable is also set when the if condition in the loop is true. It is also the result displayed in label1
I initialize it before the loop so that the first time through the loop there is a value for the comparison.