Challenge - 5 Problems
Camera Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What happens when this Flutter camera preview widget is displayed?
Consider this Flutter widget that shows a camera preview. What will the user see when this widget is rendered?
Flutter
import 'package:camera/camera.dart'; import 'package:flutter/material.dart'; class CameraPreviewWidget extends StatefulWidget { final CameraController controller; const CameraPreviewWidget({required this.controller, super.key}); @override State<CameraPreviewWidget> createState() => _CameraPreviewWidgetState(); } class _CameraPreviewWidgetState extends State<CameraPreviewWidget> { @override Widget build(BuildContext context) { if (!widget.controller.value.isInitialized) { return const Center(child: CircularProgressIndicator()); } return CameraPreview(widget.controller); } }
Attempts:
2 left
💡 Hint
Think about what happens before the camera is ready and after initialization.
✗ Incorrect
The widget first checks if the camera controller is initialized. If not, it shows a spinner. Once ready, it displays the live camera feed using CameraPreview.
❓ lifecycle
intermediate1:30remaining
What is the correct way to manage the CameraController lifecycle in Flutter?
You create a CameraController in initState. Which method should you override to properly release the camera resources?
Attempts:
2 left
💡 Hint
Think about cleaning up resources when the widget is removed permanently.
✗ Incorrect
dispose() is called when the widget is permanently removed. You should dispose the CameraController there to free camera resources.
📝 Syntax
advanced2:00remaining
What error does this Flutter camera initialization code produce?
Analyze this snippet initializing a CameraController. What error will it cause?
Flutter
late CameraController controller; void initCamera() async { final cameras = await availableCameras(); controller = CameraController(cameras.first, ResolutionPreset.high); await controller.initialize(); } @override void initState() { super.initState(); initCamera(); }
Attempts:
2 left
💡 Hint
Consider what happens when you call an async function without awaiting it in initState.
✗ Incorrect
initCamera is async but called without await in initState, so controller may be used before initialization completes, causing runtime errors.
advanced
1:30remaining
What happens if you navigate away from a screen with an active CameraController without disposing it?
If you push a new screen on top of a camera preview screen but forget to dispose the CameraController, what is the likely outcome?
Attempts:
2 left
💡 Hint
Think about resource management and what happens if you don't clean up.
✗ Incorrect
If you don't dispose the CameraController, the camera stays active, wasting battery and possibly causing conflicts or crashes.
🧠 Conceptual
expert2:30remaining
Which Flutter permission handling approach ensures camera access works on both Android and iOS?
You want to request camera permission before initializing the camera. Which approach is best to handle permissions cross-platform?
Attempts:
2 left
💡 Hint
Think about runtime permission requests on modern mobile OSes.
✗ Incorrect
Modern Android and iOS require runtime permission requests. The permission_handler package simplifies this cross-platform.