Skip to content

Instantly share code, notes, and snippets.

@austinevick
Created September 8, 2022 12:04
Show Gist options
  • Save austinevick/e87db57ab0e30aae8202a2cbe96d9488 to your computer and use it in GitHub Desktop.
Save austinevick/e87db57ab0e30aae8202a2cbe96d9488 to your computer and use it in GitHub Desktop.
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