📍 location_package
A Flutter package for obtaining the user's current location with support for offline fallback using the last known position. Built using geolocator
and permission_handler
.
✨ Features
- Get current location with configurable accuracy.
- Fallback to last known location (offline support).
- Automatic permission handling.
- Easily extendable for location-based features.
🚀 Installation
Add the following to your pubspec.yaml
:
dependencies:
location_package: ^0.0.1
Then run:
flutter pub get
⚙️ Usage
import 'package:location_package/location_package.dart';
void getUserLocation() async {
final locationService = LocationService(
accuracy: LocationAccuracy.high,
askPermissions: true,
useFallback: true,
);
final location = await locationService.getLocation();
if (location != null) {
print('Latitude: ${location.latitude}');
print('Longitude: ${location.longitude}');
} else {
print('Failed to retrieve location.');
}
}
Platform Configuration
🔧 Android Configuration
Add the following permissions to your app’s AndroidManifest.xml file located at:
android/app/src/main/AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
For Android 10+ support, update your android/app/build.gradle:
defaultConfig {
minSdkVersion 21
// ...
}
Also, ensure your compileSdkVersion and targetSdkVersion are at least 33.
🍎 iOS Configuration
In your ios/Runner/Info.plist file, add:
<key>NSLocationWhenInUseUsageDescription</key>
<string>This app needs access to your location.</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>This app needs continuous location access in the background.</string>
📦 API
LocationService constructor:
LocationService({
LocationAccuracy accuracy = LocationAccuracy.high,
bool askPermissions = true,
bool useFallback = true,
});
Methods:
Future<Position?> getLocation() – Returns best available location with fallback.
Future<Position?> getCurrentLocation() – Returns current location if permissions are granted.
Future<Position?> getLastKnownLocation() – Returns the cached last location (if any).
🛡️ License
This project is licensed under the MIT License.
📬 Contributions & Issues
Feel free to open issues or submit pull requests. Contributions are welcome!