fl_location 4.1.0+1 copy "fl_location: ^4.1.0+1" to clipboard
fl_location: ^4.1.0+1 copied to clipboard

A plugin that can access the location services of each platform and collect device location data.

fl_location #

A plugin that can access the location services of each platform and collect device location data.

Platform #

  • ✅ Android
  • ✅ iOS
  • ✅ Web

Features #

  • Can request location permission.
  • Can get the current location of the device.
  • Can check whether location services is enabled.
  • Can subscribe to LocationStream to listen location in real time.
  • Can subscribe to LocationServicesStatusStream to listen location services status changes in real time.

Getting started #

To use this plugin, add fl_location as a dependency in your pubspec.yaml file. For example:

dependencies:
  fl_location: ^4.1.0+1

After adding the fl_location plugin to the flutter project, we need to specify the platform-specific permissions for this plugin to work properly.

🐤 Android #

Since this plugin works based on location, we need to add the following permission to the AndroidManifest.xml file. Open the AndroidManifest.xml file and specify it between the <manifest> and <application> tags.

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

If you want to get the location in the background, add the following permission. If your project supports Android 10, be sure to add the ACCESS_BACKGROUND_LOCATION permission.

<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

🐤 iOS #

Like the Android platform, this plugin works based on location, we need to add the following description. Open the ios/Runner/Info.plist file and specify it inside the <dict> tag.

<key>NSLocationWhenInUseUsageDescription</key>
<string>Used to collect location data.</string>

If you want to get the location in the background, add the following description.

<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Used to collect location data in the background.</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Used to collect location data in the background.</string>
<key>UIBackgroundModes</key>
<array>
    <string>fetch</string>
    <string>location</string>
</array>

How to use #

  1. Check whether the location permission is granted or not, and if not granted, request the location permission.
Future<bool> _checkAndRequestPermission({bool? background}) async {
  if (!await FlLocation.isLocationServicesEnabled) {
    // Location services is disabled.
    return false;
  }

  LocationPermission permission = await FlLocation.checkLocationPermission();
  if (permission == LocationPermission.deniedForever) {
    // Location permission has been permanently denied.
    return false;
  } else if (permission == LocationPermission.denied) {
    // Ask the user for location permission.
    permission = await FlLocation.requestLocationPermission();
    if (permission == LocationPermission.denied ||
        permission == LocationPermission.deniedForever) {
      // Location permission has been denied.
      return false;
    }
  }

  // Location permission must always be granted (LocationPermission.always)
  // to collect location data in the background.
  if (background == true && permission == LocationPermission.whileInUse) {
    // Location permission must always be granted to collect location in the background.
    return false;
  }

  return true;
}
  1. To get the current Location, use the getLocation function.
Future<void> _getLocation() async {
  if (await _checkAndRequestPermission()) {
    final Location location = await FlLocation.getLocation();
    print('location: ${location.toJson()}');
  }
}
  1. To listen location in real time, use the getLocationStream function.
StreamSubscription<Location>? _locationSubscription;

Future<void> _subscribeLocationStream() async {
  if (await _checkAndRequestPermission()) {
    _locationSubscription = FlLocation.getLocationStream().listen(_onLocation);
  }
}

void _onLocation(Location location) {
  print('location: ${location.toJson()}');
}
  1. To listen location services status changes in real time, use the getLocationServicesStatusStream function.
StreamSubscription<LocationServicesStatus>? _locationServicesStatusSubscription;

Future<void> _subscribeLocationServicesStatusStream() async {
  _locationServicesStatusSubscription = FlLocation.getLocationServicesStatusStream()
      .listen(_onLocationServicesStatus);
}

void _onLocationServicesStatus(LocationServicesStatus status) {
  print('LocationServicesStatus: $status');
}

Background Mode #

If you want to use this plugin in the background, use the flutter_foreground_task plugin.

demo: https://github.com/Dev-hwang/flutter_foreground_task_example/tree/main/location_service

More Documentation #

Go here to learn about the models provided by this plugin.

Go here to learn about the utility provided by this plugin.

Go here to migrate to the new version.

Support #

If you find any bugs or issues while using the plugin, please register an issues on GitHub. You can also contact us at hwj930513@naver.com.

41
likes
0
pub points
94%
popularity

Publisher

verified publisherpravera.me

A plugin that can access the location services of each platform and collect device location data.

Homepage
Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

fl_location_platform_interface, fl_location_web, flutter

More

Packages that depend on fl_location