capture location on foreground in Flutter Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of capture location on foreground in Flutter without wasting too much if your time.

The question is published on by Tutorial Guruji team.

Is there any official way to capture location on the foreground in Flutter? Or Please give your feedback for my way is okay or not. (both Android and IOS)

The trip will start if the user clicks the start button. The trip will finish if the user clicks the end button.

Currently, I am storing current geo point to the Array

 List<GeoPoint> tripGeoPoint = [];

  Widget build(BuildContext context) {
    //Stream location data using this plugin -> `location`
    UserLocation userLocation = Provider.of<UserLocation>(context);

    if (startTrip) {
      tripGeoPoint.add(GeoPoint(userLocation.latitude, userLocation.longitude));

I am currently using this way to capture location on the foreground. (I think this is not the right solution for capture location on the foreground)

  Timer _timer;

  void didChangeAppLifecycleState(AppLifecycleState state) {
    switch (state) {
      case AppLifecycleState.resumed:
          if (_timer != null) _timer.cancel();
          print("app in resumed");
      case AppLifecycleState.inactive://foreground
          print("app in inactive");
          const oneSec = const Duration(seconds: 3);
          _timer = Timer.periodic(oneSec, (Timer t) async {
            UserLocation _loc = await getLocation();
            GeoPoint geoPoint = GeoPoint(_loc.latitude, _loc.longitude);
      case AppLifecycleState.paused:
      case AppLifecycleState.detached:

Top-Level function for capture location on foreground

var location = Location();
UserLocation _currentLocation;

Future<UserLocation> getLocation() async {
  try {
    var userLocation = await location.getLocation();
    _currentLocation = UserLocation(latitude: userLocation.latitude, longitude: userLocation.longitude);
  } on Exception catch (e) {
    print('Could not get location: $e');
  return _currentLocation;


I found two plugins to achieve this.

(paid) Currently, I am using this plugin for production projects flutter_background_geolocation

alternative (free) background_locator

We are here to answer your question about capture location on foreground in Flutter - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji