Future Builder not working with Geolocator with flutter


I am a little new to flutter and I am using Geolocator plugin for the first time. I am trying to have a FutureBuilder that finds the city of the user when built but it never leaves the loading screen

this is my current code:

 class Home extends StatefulWidget {
  _HomeState createState() => _HomeState();

class _HomeState extends State<Home> {

String currentTown;

  Widget build(BuildContext context) {
    return FutureBuilder(
      future: _getTown(),
        builder: (context, snap) {
        if(snap.connectionState == ConnectionState.waiting) {
          return Loading();
        } else {
          return Center(
            child: Text(
              currentTown ?? null,
              style: TextStyle(
                color: Colors.white

  Future _getTown() async {
    print('get Town');
    Position pos = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
    final coordinates = Coordinates(pos.latitude, pos.longitude);
    var address = await Geocoder.local.findAddressesFromCoordinates(coordinates);
    setState(() {
      currentTown = address.first.locality;



  geolocator: ^7.0.1
  geocoder: ^0.2.1

I’m not exactly sure what I am doing wrong. I tried having the future return the String value but it still wouldn’t change from the loading screen. even when I use ConnectionState it still doesn’t work


you need to return something try this

Future _getTown() async {
       return address.first.locality;

also modify your builder

 builder: (context, snapshot) {
            if (snapshot.connectionState == ConnectionState.done) {
              if (snapshot.hasData) {
     else{return loadingWidget();}

Answered By – Dean Villamia

Answer Checked By – Cary Denson (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *