fl_location

Creator: coderz1093

Last updated:

Add to Cart

Description:

fl location

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
copied to clipboard
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" />
copied to clipboard
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" />
copied to clipboard
🐤 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>
copied to clipboard
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>
copied to clipboard
How to use #

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;
}
copied to clipboard

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()}');
}
}
copied to clipboard

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()}');
}
copied to clipboard

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');
}
copied to clipboard
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.

License

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Customer Reviews

There are no reviews.