権限とセンサー
Flutter で実行時権限をリクエストし、加速度センサーなどのデバイスセンサーを読み取って、状況に応じて動作するプラットフォーム統合アプリを構築します。
「権限とセンサー」はCoddyKit上の無料Flutter Mobile Developmentレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはFlutter Mobile Development学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Flutter Mobile Developmentコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Why Permissions & Sensors?
Many platform features — camera, location, sensors — require runtime permission. And sensors like the accelerometer let your app react to physical movement.
The permission_handler Package
The permission_handler package gives a unified API for requesting and checking permissions across Android and iOS.
dependencies:
permission_handler: ^11.0.0Checking Status
Check whether a permission is granted before using a feature.
final status = await Permission.camera.status;
if (status.isGranted) {
openCamera();
}Requesting a Permission
Call request() to prompt the user. Always handle a denial gracefully.
final result = await Permission.location.request();
if (result.isGranted) {
startTracking();
}Permanently Denied
If the user permanently denies, you cannot re-prompt — send them to settings instead.
if (status.isPermanentlyDenied) {
await openAppSettings();
}Declaring Permissions
You must also declare permissions natively: AndroidManifest.xml on Android and Info.plist usage descriptions on iOS.
<!-- Android -->
<uses-permission android:name="android.permission.CAMERA" />
<!-- iOS Info.plist -->
<key>NSCameraUsageDescription</key>
<string>We need the camera to scan codes</string>Meet the Sensors
The sensors_plus package exposes device motion sensors as streams.
- Accelerometer
- Gyroscope
- Magnetometer
dependencies:
sensors_plus: ^4.0.0Reading the Accelerometer
Subscribe to the accelerometer stream to get x, y, z acceleration values.
accelerometerEventStream().listen((event) {
print('x: ' + event.x.toString() + ' y: ' + event.y.toString());
});The Gyroscope
The gyroscope reports rotation rate around each axis — useful for tilt and orientation effects.
gyroscopeEventStream().listen((event) {
print('rotation: ' + event.x.toString());
});Detecting a Shake
Combine accelerometer axes into a magnitude and compare against a threshold to detect a shake gesture.
import 'dart:math';
void onEvent(AccelerometerEvent e) {
final magnitude = sqrt(e.x * e.x + e.y * e.y + e.z * e.z);
if (magnitude > 15) onShake();
}Cleaning Up Subscriptions
Sensor streams keep firing — cancel the subscription in dispose to save battery.
late StreamSubscription sub;
@override
void dispose() {
sub.cancel();
super.dispose();
}Quick Check
The user permanently denied location. What should your app do?
Recap
You learned permissions and sensors:
- permission_handler: status, request, openAppSettings
- Native declarations on Android and iOS
- sensors_plus accelerometer and gyroscope streams
- Shake detection and cleanup
Your apps can now responsibly access platform capabilities.
よくある質問
「権限とセンサー」レッスンは無料ですか?
はい。「権限とセンサー」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Flutter Mobile Developmentコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Flutter Mobile Developmentコースには全4レッスンが含まれています。
「権限とセンサー」で何を学びますか?
Flutter で実行時権限をリクエストし、加速度センサーなどのデバイスセンサーを読み取って、状況に応じて動作するプラットフォーム統合アプリを構築します。 ブラウザで直接実行するハンズオンコードでFlutter Mobile Developmentを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Flutter Mobile Developmentを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのFlutter Mobile Developmentは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「権限とセンサー」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このFlutter Mobile Developmentレッスンでコードを書いて実行できますか?
はい。すべてのFlutter Mobile Developmentレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。