Created
September 8, 2022 12:04
-
-
Save austinevick/e87db57ab0e30aae8202a2cbe96d9488 to your computer and use it in GitHub Desktop.
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 'package:flutter_riverpod/flutter_riverpod.dart'; | |
import 'package:location/location.dart'; | |
final landingViewProvider = | |
StateNotifierProvider<LandingViewModel, bool>((ref) => LandingViewModel()); | |
class LandingViewModel extends StateNotifier<bool> { | |
LandingViewModel() : super(false); | |
Future<bool> getUserLocation() async { | |
try { | |
state = true; | |
bool serviceEnabled = await Location.instance.serviceEnabled(); | |
if (!serviceEnabled) { | |
bool isRequestGranted = await Location.instance.requestService(); | |
if (!isRequestGranted) { | |
return false; | |
} | |
} | |
PermissionStatus status = await Location.instance.hasPermission(); | |
if (status == PermissionStatus.denied) { | |
status = await Location.instance.requestPermission(); | |
if (status != PermissionStatus.granted) { | |
return true; | |
} | |
} | |
state = false; | |
return true; | |
} catch (e) { | |
state = false; | |
rethrow; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment