0

I know why it returns this but I don't know how to get the actual value that I want

Future<String> _getAddress(double? lat, double? lang) async {
if (lat == null || lang == null) return "";
GeoCode geoCode = GeoCode();
Address address =
await geoCode.reverseGeocoding(latitude: lat, longitude: lang);
return "${address.streetAddress}, ${address.city}, ${address.countryName}, ${address.postal}";

}

Text(_getAddress(lat, lon).toString())

2 Answers2

0

You have to await this. You can see more information on dart.dev - async/await.

To use this in a Widget, you have to make it Stateful and use initState() to assign it to a variable. You can make it nullable String? address and add an if inside your build method to check if it has a value (show your Text widget) or null (show for example a CircularProgressIndicator).

0

You can use FutureBuilder:

FutureBuilder<String>(
    future: _getAddress(lat, lon),
    builder: (
        BuildContext context,
        AsyncSnapshot<String> snapshot,
    ) {
        if (snapshot.hasData) {
            return Text(snapshot.data);
        } else {
            return Text('Loading data');
        }
    },  
)
MARK
  • 13
  • 1
  • 6