Challenge - 5 Problems
File Storage Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
Reading a file's content in Flutter
What will be the output shown on the screen after running this Flutter code snippet that reads a file's content asynchronously?
Flutter
import 'package:flutter/material.dart'; import 'dart:io'; import 'package:path_provider/path_provider.dart'; class FileReadExample extends StatefulWidget { @override State<FileReadExample> createState() => _FileReadExampleState(); } class _FileReadExampleState extends State<FileReadExample> { String _content = 'Loading...'; @override void initState() { super.initState(); _readFile(); } Future<void> _readFile() async { final dir = await getApplicationDocumentsDirectory(); final file = File('${dir.path}/myfile.txt'); if (await file.exists()) { final text = await file.readAsString(); setState(() { _content = text; }); } else { setState(() { _content = 'File not found'; }); } } @override Widget build(BuildContext context) { return Scaffold( body: Center(child: Text(_content)), ); } }
Attempts:
2 left
💡 Hint
Check how the file existence is checked and how setState updates the UI.
✗ Incorrect
The code reads the file asynchronously and updates the UI with the file content or a 'File not found' message. The setState call triggers the UI update.
❓ lifecycle
intermediate2:00remaining
File writing and app lifecycle
In a Flutter app, where is the best place to save user data to a file to ensure it is stored before the app closes?
Attempts:
2 left
💡 Hint
Think about app lifecycle events and when the app is about to go to background.
✗ Incorrect
The didChangeAppLifecycleState() method lets you detect when the app is paused or inactive, which is the right time to save data to persistent storage.
📝 Syntax
advanced2:00remaining
Correct syntax for writing to a file asynchronously
Which option correctly writes the string 'Hello Flutter' to a file asynchronously in Flutter?
Flutter
import 'dart:io'; import 'package:path_provider/path_provider.dart'; Future<void> writeFile() async { final dir = await getApplicationDocumentsDirectory(); final file = File('${dir.path}/hello.txt'); // Write string to file here }
Attempts:
2 left
💡 Hint
Check the exact method name and whether it returns a Future.
✗ Incorrect
The correct method is writeAsString which returns a Future, so it must be awaited for asynchronous completion.
🔧 Debug
advanced2:00remaining
Why does this file read code throw an exception?
Given this Flutter code snippet, what is the cause of the exception thrown when reading the file?
Flutter
final file = File('myfile.txt'); final content = await file.readAsString();
Attempts:
2 left
💡 Hint
Consider how file paths work on mobile devices.
✗ Incorrect
Using a relative path like 'myfile.txt' does not point to a valid file location on mobile devices. You must use absolute paths from directories like getApplicationDocumentsDirectory().
🧠 Conceptual
expert2:00remaining
Understanding file storage permissions on mobile
Which statement best describes file storage permissions for Flutter apps on Android and iOS?
Attempts:
2 left
💡 Hint
Think about app sandboxing and default permissions.
✗ Incorrect
Mobile OS sandboxing restricts apps to their own directories. Apps can read/write there without extra permissions, but need permissions for shared storage.