0
0
Fluttermobile~20 mins

File storage in Flutter - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
File Storage Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2: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)),
    );
  }
}
AThe screen shows 'Loading...' forever because setState is not called.
BThe screen shows the text content of 'myfile.txt' if it exists, otherwise 'File not found'.
CThe app crashes because getApplicationDocumentsDirectory() is not awaited.
DThe screen shows an empty string because the file is read but not assigned.
Attempts:
2 left
💡 Hint
Check how the file existence is checked and how setState updates the UI.
lifecycle
intermediate
2: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?
AInside the build() method of a StatefulWidget.
BInside the initState() method of a StatefulWidget.
CInside the didChangeAppLifecycleState() method when the app state is paused or inactive.
DInside the dispose() method of a StatefulWidget.
Attempts:
2 left
💡 Hint
Think about app lifecycle events and when the app is about to go to background.
📝 Syntax
advanced
2: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
}
Aawait file.writeAsString('Hello Flutter');
Bfile.writeAsString('Hello Flutter');
Cfile.writeString('Hello Flutter');
Dawait file.writeString('Hello Flutter');
Attempts:
2 left
💡 Hint
Check the exact method name and whether it returns a Future.
🔧 Debug
advanced
2: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();
AThe readAsString() method does not exist on File objects.
BThe File class cannot be used in Flutter apps.
CThe await keyword cannot be used outside a build method.
DThe file path is relative and does not point to a valid location on the device.
Attempts:
2 left
💡 Hint
Consider how file paths work on mobile devices.
🧠 Conceptual
expert
2:00remaining
Understanding file storage permissions on mobile
Which statement best describes file storage permissions for Flutter apps on Android and iOS?
AFlutter apps can only read and write files within their own app-specific directories without extra permissions.
BFlutter apps cannot write files to the device storage at all.
CFlutter apps must request user permission to write files inside their own app directory.
DFlutter apps can freely read and write any file on the device without restrictions.
Attempts:
2 left
💡 Hint
Think about app sandboxing and default permissions.