Bird
Raised Fist0
Fluttermobile~20 mins

Permissions handling in Flutter - Mini App: Build & Ship

Choose your learning style10 modes available
Build: Permissions Screen
This screen asks the user for camera permission and shows the current permission status.
Target UI
Permissions Screen

[Request Camera Permission]

Status: Unknown
Add a button labeled 'Request Camera Permission'.
When tapped, request camera permission from the user.
Display the current permission status below the button.
Handle permission granted, denied, and permanently denied states.
Use the 'permission_handler' package.
Starter Code
Flutter
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';

class PermissionsScreen extends StatefulWidget {
  @override
  State<PermissionsScreen> createState() => _PermissionsScreenState();
}

class _PermissionsScreenState extends State<PermissionsScreen> {
  String _status = 'Unknown';

  void _requestPermission() async {
    // TODO: Request camera permission and update _status
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Permissions Screen')),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(
              onPressed: _requestPermission,
              child: Text('Request Camera Permission'),
            ),
            SizedBox(height: 20),
            Text('Status: $_status'),
          ],
        ),
      ),
    );
  }
}
Task 1
Task 2
Task 3
Solution
Flutter
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';

class PermissionsScreen extends StatefulWidget {
  @override
  State<PermissionsScreen> createState() => _PermissionsScreenState();
}

class _PermissionsScreenState extends State<PermissionsScreen> {
  String _status = 'Unknown';

  void _requestPermission() async {
    final status = await Permission.camera.request();
    String newStatus;
    if (status.isGranted) {
      newStatus = 'Granted';
    } else if (status.isDenied) {
      newStatus = 'Denied';
    } else if (status.isPermanentlyDenied) {
      newStatus = 'Permanently Denied';
    } else {
      newStatus = 'Unknown';
    }
    setState(() {
      _status = newStatus;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Permissions Screen')),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(
              onPressed: _requestPermission,
              child: Text('Request Camera Permission'),
            ),
            SizedBox(height: 20),
            Text('Status: $_status'),
          ],
        ),
      ),
    );
  }
}

We use the permission_handler package to ask for camera permission.

When the button is pressed, _requestPermission runs. It requests camera permission and waits for the result.

Based on the result, we update the _status string to show if permission is granted, denied, or permanently denied.

Calling setState refreshes the screen to show the new status.

Final Result
Completed Screen
Permissions Screen

[Request Camera Permission]

Status: Granted
User taps 'Request Camera Permission' button.
App asks for camera permission from the system.
Status text updates to 'Granted', 'Denied', or 'Permanently Denied' based on user choice.
Stretch Goal
Add a button to open app settings if permission is permanently denied.
💡 Hint
Use Permission.camera.openAppSettings() to open settings.

Practice

(1/5)
1. What is the main purpose of handling permissions in a Flutter app?
easy
A. To create animations for UI elements
B. To safely access sensitive device features like camera or location
C. To change the app's theme dynamically
D. To improve app performance by caching data

Solution

  1. Step 1: Understand app permissions

    Permissions allow apps to access sensitive features like camera, microphone, or location safely.
  2. Step 2: Identify the purpose in Flutter apps

    Handling permissions ensures the app asks user consent before accessing these features.
  3. Final Answer:

    To safely access sensitive device features like camera or location -> Option B
  4. Quick Check:

    Permissions = Safe access to sensitive features [OK]
Hint: Permissions control access to device features safely [OK]
Common Mistakes:
  • Confusing permissions with UI customization
  • Thinking permissions improve performance
  • Assuming permissions are for animations
2. Which Flutter package is commonly used to check and request permissions?
easy
A. http
B. provider
C. permission_handler
D. shared_preferences

Solution

  1. Step 1: Identify permission packages

    The permission_handler package is designed to manage permissions in Flutter.
  2. Step 2: Exclude unrelated packages

    http is for network calls, provider for state management, and shared_preferences for local storage.
  3. Final Answer:

    permission_handler -> Option C
  4. Quick Check:

    Permission package = permission_handler [OK]
Hint: permission_handler manages permissions in Flutter [OK]
Common Mistakes:
  • Choosing http for permissions
  • Confusing provider with permission handling
  • Using shared_preferences for permissions
3. What will the following Flutter code print if the camera permission is granted?
var status = await Permission.camera.status;
if (status.isGranted) {
  print('Camera permission granted');
} else {
  print('Camera permission denied');
}
medium
A. Syntax error
B. Camera permission denied
C. Permission status unknown
D. Camera permission granted

Solution

  1. Step 1: Check permission status

    The code checks if camera permission is granted using status.isGranted.
  2. Step 2: Determine output for granted permission

    If granted, it prints 'Camera permission granted'. Otherwise, it prints denial message.
  3. Final Answer:

    Camera permission granted -> Option D
  4. Quick Check:

    Granted permission prints granted message [OK]
Hint: isGranted true means permission granted [OK]
Common Mistakes:
  • Confusing isGranted with isDenied
  • Expecting syntax error
  • Ignoring async await usage
4. Identify the error in this Flutter code snippet for requesting location permission:
var status = await Permission.location.request();
if (status == PermissionStatus.granted) {
  print('Permission granted');
} else {
  print('Permission denied');
}
medium
A. No error, code is correct
B. Using == to compare PermissionStatus instead of isGranted property
C. Permission.location.request() does not exist
D. Missing await keyword before request()

Solution

  1. Step 1: Review permission request syntax

    The request() method returns a PermissionStatus which can be compared using ==.
  2. Step 2: Confirm code correctness

    The code correctly awaits the request and compares status to PermissionStatus.granted.
  3. Final Answer:

    No error, code is correct -> Option A
  4. Quick Check:

    Permission request syntax is correct [OK]
Hint: PermissionStatus can be compared with == safely [OK]
Common Mistakes:
  • Thinking isGranted must be used instead of ==
  • Forgetting await before request()
  • Assuming request() method does not exist
5. You want to show a message to the user only if the app has both camera and microphone permissions granted. Which Flutter code snippet correctly checks this using permission_handler?
hard
A. if (await Permission.camera.isGranted && await Permission.microphone.isGranted) { print('All permissions granted'); }
B. if (await Permission.camera.request() && await Permission.microphone.request()) { print('All permissions granted'); }
C. if (Permission.camera.isGranted && Permission.microphone.isGranted) { print('All permissions granted'); }
D. if (Permission.camera.status.isGranted && Permission.microphone.status.isGranted) { print('All permissions granted'); }

Solution

  1. Step 1: Understand async permission checks

    Checking isGranted requires awaiting because it returns a Future<bool>.
  2. Step 2: Analyze each option

    if (await Permission.camera.isGranted && await Permission.microphone.isGranted) { print('All permissions granted'); } correctly awaits both permissions' isGranted properties. if (await Permission.camera.request() && await Permission.microphone.request()) { print('All permissions granted'); } incorrectly awaits request() which returns PermissionStatus, not bool. if (Permission.camera.isGranted && Permission.microphone.isGranted) { print('All permissions granted'); } misses await, causing errors. if (Permission.camera.status.isGranted && Permission.microphone.status.isGranted) { print('All permissions granted'); } accesses status without await, which is a Future.
  3. Final Answer:

    if (await Permission.camera.isGranted && await Permission.microphone.isGranted) { print('All permissions granted'); } -> Option A
  4. Quick Check:

    Await isGranted for correct permission check [OK]
Hint: Await isGranted to check permission status correctly [OK]
Common Mistakes:
  • Not awaiting async permission checks
  • Using request() instead of isGranted for condition
  • Accessing status without await
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard