Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the SharedPreferences package.
Flutter
import 'package:[1]/shared_preferences.dart';
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect package names like 'flutter' or 'prefs'.
Forgetting to import the package.
✗ Incorrect
The correct package name to import SharedPreferences is shared_preferences.
2fill in blank
mediumComplete the code to get an instance of SharedPreferences asynchronously.
Flutter
final prefs = await SharedPreferences.[1](); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'instance' or 'init' which do not exist.
Not using await for the asynchronous call.
✗ Incorrect
The method to get SharedPreferences instance is getInstance().
3fill in blank
hardFix the error in saving a string value with key 'username'.
Flutter
await prefs.[1]('username', 'Alice');
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-existent methods like 'saveString' or 'putString'.
Confusing with other storage APIs.
✗ Incorrect
The correct method to save a string is setString.
4fill in blank
hardFill both blanks to read a string value for key 'username' with a default fallback.
Flutter
String username = prefs.[1]('username') ?? [2];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'get' instead of 'getString'.
Not providing a default value causing null errors.
✗ Incorrect
Use getString to read the string and provide a default like 'Guest' if null.
5fill in blank
hardFill all three blanks to remove a key and check if it was removed successfully.
Flutter
bool removed = await prefs.[1]('username'); if (removed) { print('[2] was removed'); } else { print('[3] not found'); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong method names like 'delete'.
Using incorrect key names in messages.
✗ Incorrect
The method to remove a key is remove. The key name is username. The message uses 'Key' to indicate the key was not found.