How to await on a function before Build in a stateful widget?

458

You should use the FutureBuilder widget where the future is your async function and builder is whatever you want to return (the scaffold in your case). Check if the future has returned anything with snapshot.hasData and return a progress indicator (or anything you want) if it hasn't returned anything.

    FutureBuilder(
      future: getPos(),
      builder: (context, snapshot) {
    snapshot.hasData ? 
      Scaffold(
                appBar: AppBar(
                  title: Text('Distancing Alerts'),
                  backgroundColor: Colors.red[800],
        ),
        body: Padding(
                    padding: const EdgeInsets.all(36.0),
                    child: Column(
                      children: [
                        Text(
                          'YOUR CURRENT POSITION',
                          style: TextStyle(
                            color: Colors.red[800],
                          ),
                        ), //THE ERROR OCCURS HERE
                        Text(position.latitude.toString() +
                            '° N , ' +
                            position.longitude.toString() +
                            '° E'),
        ],
                    ))) : CircularProgressIndicator();};
Share:
458
Mithil Chaudhary
Author by

Mithil Chaudhary

Updated on December 26, 2022

Comments

  • Mithil Chaudhary
    Mithil Chaudhary over 1 year

    I'm awaiting a GEOLOCATOR function to fetch location from the user. But during the initial build I always get an error(latitude was called on null) for a few seconds before the location is actually retrieved from the GEOLOCATOR. How do I make it so that the LOCATION is actually fetched before the page is built?

    import 'package:flutter/material.dart';
    import 'package:geolocator/geolocator.dart';
    
    class DistancingPage extends StatefulWidget {
      DistancingPage(this.uid);
      @override
      _DistancingPageState createState() => _DistancingPageState();
    }
    
    class _DistancingPageState extends State<DistancingPage> {
    Position position;
    
    @override
      void initState() {
        super.initState();
        getPos();
      }
    
      Future<void> getPos() async {
        Position p = await Geolocator.getCurrentPosition(
            desiredAccuracy: LocationAccuracy.high);
    
        setState(() {
          position = p;
        });
      }
    @override
      Widget build(BuildContext context) {
    return Scaffold(
            appBar: AppBar(
              title: Text('Distancing Alerts'),
              backgroundColor: Colors.red[800],
    ),
    body: Padding(
                padding: const EdgeInsets.all(36.0),
                child: Column(
                  children: [
                    Text(
                      'YOUR CURRENT POSITION',
                      style: TextStyle(
                        color: Colors.red[800],
                      ),
                    ), //THE ERROR OCCURS HERE
                    Text(position.latitude.toString() +
                        '° N , ' +
                        position.longitude.toString() +
                        '° E'),
    ],
                )));
      }
    }
    
    
    
    • Yadu
      Yadu over 3 years
      you cannot do that, you cant wait for future while building a UI, but you can update the UI once the data is available, use a FutureBuilder for that
  • Ox. S
    Ox. S over 3 years
    Or you can fetch the location the background if the app is in foreground, there is article how to do it medium.com/@pierre.sabot/…