0
0
Fluttermobile~10 mins

Why local storage enables offline data in Flutter - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to save a string value to local storage using Flutter's SharedPreferences.

Flutter
final prefs = await SharedPreferences.getInstance();
await prefs.setString('username', [1]);
Drag options to blanks, or click blank then click option'
AJohnDoe
B"JohnDoe"
C12345
Dtrue
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put quotes around the string value.
Passing a variable name without quotes if it's not defined.
2fill in blank
medium

Complete the code to read a string value from local storage using Flutter's SharedPreferences.

Flutter
final prefs = await SharedPreferences.getInstance();
String? username = prefs.[1]('username');
Drag options to blanks, or click blank then click option'
AgetString
Bremove
CsetString
DgetInt
Attempts:
3 left
💡 Hint
Common Mistakes
Using setString instead of getString when reading data.
Using getInt when the stored value is a string.
3fill in blank
hard

Fix the error in the code to check if local storage has a key before reading it.

Flutter
final prefs = await SharedPreferences.getInstance();
if (prefs.[1]('username')) {
  String? username = prefs.getString('username');
}
Drag options to blanks, or click blank then click option'
AhasKey
Bexists
CcheckKey
DcontainsKey
Attempts:
3 left
💡 Hint
Common Mistakes
Using hasKey or exists which do not exist in SharedPreferences.
Trying to read a key without checking if it exists.
4fill in blank
hard

Fill both blanks to save and then read an integer value from local storage.

Flutter
final prefs = await SharedPreferences.getInstance();
await prefs.[1]('counter', 10);
int? counter = prefs.[2]('counter');
Drag options to blanks, or click blank then click option'
AsetInt
BgetString
CgetInt
DsetString
Attempts:
3 left
💡 Hint
Common Mistakes
Using setString or getString for integer values.
Mixing up the order of saving and reading methods.
5fill in blank
hard

Fill both blanks to remove a key from local storage and check if it still exists.

Flutter
final prefs = await SharedPreferences.getInstance();
await prefs.[1]('session');
bool exists = prefs.[2]('session');
print('Session key exists: ' + exists.toString());
Drag options to blanks, or click blank then click option'
Aremove
BcontainsKey
CgetBool
Dclear
Attempts:
3 left
💡 Hint
Common Mistakes
Using clear which deletes all keys instead of remove.
Using getBool to check if a key exists.