Keep reviving a null value after calling a future function, but the data is clearly there

72 Views Asked by At

I am trying to use a geolocator and then put the values from the data received from a plug in into a list so I can then sort it. The problem is that when I try to add said data to the list, I get an error saying that the value being added is null, and then another one saying that I tried to call add(whatever number I tried to add to the list was) I know that it is because future variables and regular variables are treated differently. Does anyone know how to convert a future variable to a normal one? The async function and main code are as follows

***The following code is my main code, it creates a list of widgets, I am trying to organize them so that the smallest value is first- however I am organizing them by distance, and I cannot get the distance without a future function

 return ListView( 
            //This part works just grabs my firebase documents
            children: snapshot.data.docs.map((document) { 
              //index++;
             GeoPoint tempVar = document['location'];
//This part does not work
                List<int> widgetIds;
          Future<double> listValue = getDistance(tempVar);
       listValue.then((double future) {
          if(listValue != null){
         
           var newV = myList(listValue.round());
           print('$newV');
          }
          if(listValue == null){}
         
             });
              //trashcans[index].description = document['description'];
              // List<double> widgetIds = newVar;
              // print(widgetIds);
//this part works, puts them in into a custom widget
              return canTemplate(document, context,);
             
              // return Container( 
              //   child: Center(child: Text(document['description'] + " - " + document['full']),), 
            }).toList(), 
          ); 

Here is my getDistance() function, it works with the normal template that I had, but now that I am trying to save the values in variables it does not work



Future <double> getDistance(GeoPoint loc) async {
  Position position = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.high); 
  double lat1 = position.latitude;
  double long1 = position.longitude;
  double lat2 = loc.latitude;
  double long2 = loc.longitude;
  double distanceInMeters = await Geolocator().distanceBetween(lat1, long1, lat2, long2);
  double feet = distanceInMeters * 3.28084;
 

  return feet;
}

Here is the myList function, I thought that if I could create the list as a future variable it would solve the issue but it doesnt work

Future <List> myList(int data) async{
  List widgetIds;
  widgetIds.add(data);
  return widgetIds;
}

These are the errors I get /flutter ( 5177): Closure: (int) => Future<List> from Function 'myList': static. E/flutter ( 5177): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: NoSuchMethodError: The method 'add' was called on null. E/flutter ( 5177): Receiver: null E/flutter ( 5177): Tried calling: add(1046046)

As you can see it has the number I want, but for some reason the value is still considered null

for some reason when I use the await call, for examples instead of doing

var newV = myList(listValue.round());

I change it to this

var newV = await myList(listValue.round());

It states that await can only be used on a future and not a double

0

There are 0 best solutions below