Last active
May 26, 2020 16:48
-
-
Save freedive-cebu30/12f88225c8fff474ddb6bc6f432b6fee to your computer and use it in GitHub Desktop.
permission_handler_1
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:location/location.dart'; | |
import 'package:permission_handler/permission_handler.dart' as per_handler; | |
void main() { | |
runApp( | |
ExchangeShop(), | |
); | |
} | |
class MapCebu extends StatefulWidget { | |
@override | |
State<MapCebu> createState() => MapCebuState(); | |
} | |
class MapCebuState extends State<MapCebu> { | |
LocationData currentLocation; | |
Location _locationService = Location(); | |
@override | |
void initState() { | |
super.initState(); | |
initPlatformState(); | |
} | |
void initPlatformState() async { | |
LocationData myLocation; | |
try { | |
myLocation = await _locationService.getLocation(); | |
} catch (e) { | |
if (e.code == 'PERMISSION_DENIED') | |
error = 'Permission denited'; | |
else if (e.code == 'PERMISSION_DENITED_NEVER_ASK') | |
error = | |
'Permission denited - please ask the user to enable it from the app settings'; | |
bool isShown = | |
await per_handler.Permission.contacts.shouldShowRequestRationale; | |
if (isShown == false) { | |
per_handler.openAppSettings(); | |
sleep(new Duration(seconds: 2)); | |
} | |
// 位置情報の権限がない場合は、アプリを使えない仕様にする | |
exit(0); | |
} | |
setState(() { | |
currentLocation = myLocation; | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment