In this example, we want to find the user location coordinates and show them on the screen as soon as they are ready. Getting the coordinates is an asynchronous operation that returns a Future. Follow these steps:
- Create a new file called geolocation.dart in the lib folder of your project.
- Create a new stateful widget, called LocationScreen.
- In the State class of the Geolocation widget, add the code that shows the user their current position. The final result is shown here:
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
class LocationScreen extends StatefulWidget {
@override
_LocationScreenState createState() => _LocationScreenState();
}
class _LocationScreenState extends State<LocationScreen> {
String myPosition = '';
@override
void initState() {
getPosition().then((Position myPos) {
myPosition = 'Latitude: ' + myPos.latitude.toString() + ' - Longitude: ' + myPos...