From ef8c5e9039934b77fd8c77d93193695181772eb2 Mon Sep 17 00:00:00 2001 From: bytedream Date: Wed, 27 Mar 2024 02:49:11 +0100 Subject: [PATCH] check if location is activated on startup --- lib/view/home.dart | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/view/home.dart b/lib/view/home.dart index 3449052..5ef2d81 100644 --- a/lib/view/home.dart +++ b/lib/view/home.dart @@ -23,10 +23,10 @@ class _HomePageState extends State { bool locationEnabled = false; Future getLatLng() async { - LocationPermission permissionStatus = await Geolocator.checkPermission(); - if (permissionStatus == LocationPermission.denied) { - permissionStatus = await Geolocator.requestPermission(); - if (permissionStatus == LocationPermission.denied || permissionStatus == LocationPermission.deniedForever) { + LocationPermission permission = await Geolocator.checkPermission(); + if (permission == LocationPermission.denied) { + permission = await Geolocator.requestPermission(); + if (permission == LocationPermission.denied || permission == LocationPermission.deniedForever) { return null; } } @@ -48,6 +48,16 @@ class _HomePageState extends State { return null; } + + @override + void initState() { + super.initState(); + + Geolocator.checkPermission().then((permission) => setState(() { + locationEnabled = permission != LocationPermission.denied && permission != LocationPermission.deniedForever; + })); + } + @override Widget build(BuildContext context) { return Scaffold(