-
-
Save kururu-abdo/6be6fa9af13a5a8dbcd67fd281b670db to your computer and use it in GitHub Desktop.
Location provider for flutter projects, Riverpod, Location
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
import 'package:flutter/cupertino.dart'; | |
import 'package:hooks_riverpod/hooks_riverpod.dart'; | |
import 'package:location/location.dart'; | |
class PersonLocationProvider extends ChangeNotifier { | |
Location _location = new Location(); | |
PermissionStatus _permissionGranted; | |
StreamController<LocationData> currentLocation = StreamController.broadcast(); | |
PersonLocationProvider() { | |
_init(); | |
} | |
_checkPermission() async { | |
_permissionGranted = await _location.hasPermission(); | |
if (_permissionGranted == PermissionStatus.denied) { | |
_permissionGranted = await _location.requestPermission(); | |
if (_permissionGranted != PermissionStatus.granted) { | |
return; | |
} | |
} | |
} | |
_init() { | |
_checkPermission(); | |
currentLocation.addStream(_location.onLocationChanged); | |
} | |
} | |
final locationProvider = ChangeNotifierProvider<PersonLocationProvider>((ref) { | |
return PersonLocationProvider(); | |
}); | |
final locationStreamProvider = StreamProvider.autoDispose<LocationData>( | |
(ref) { | |
ref.maintainState = true; | |
final stream = ref.read(locationProvider).currentLocation.stream; | |
return stream; | |
}, | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment