Home > Net >  How to auto dismiss an AlertDialog box after getting GPS point?
How to auto dismiss an AlertDialog box after getting GPS point?

Time:04-12

I am trying to make a dialog box that displays "text" after getting the location and then doing... Navigator.of(context).pop(); to auto close the dialog

Help! I am starting at Flutter... here's the code

import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';

......

showProgressDialogGPS(BuildContext context,
    {String message = 'Loading...'}) {
  showDialog(
    context: context,
    barrierDismissible: false,
    builder: (context) {
      Future.delayed(Duration(milliseconds: 50), () {
        _getPosition();
        Navigator.of(context).pop();
      });
      return AlertDialog(
        actions: [],
        content: Row(
          children: <Widget>[
            CircularProgressIndicator(),
            SizedBox(width: 20),
            Expanded(child: Text(message)),
          ],
        ),
      );
    },
  );
}

Future _getPosition() async {
  await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.low)
      .then((value) {
    print(value);
  });
}


.......

Thanks guys!!!

CodePudding user response:

You can modify your code like this :

 Future.delayed(Duration(milliseconds: 50), () async{
        await _getPosition();
        Navigator.of(context).pop();
      });

here you are waiting for _getPosition to be executed then the statement Navigator.of(context).pop(); will be executed.

  • Related