Skip to content

Instantly share code, notes, and snippets.

@freedive-cebu30
Last active May 26, 2020 16:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save freedive-cebu30/12f88225c8fff474ddb6bc6f432b6fee to your computer and use it in GitHub Desktop.
Save freedive-cebu30/12f88225c8fff474ddb6bc6f432b6fee to your computer and use it in GitHub Desktop.
permission_handler_1
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