0
0
Fluttermobile~20 mins

Realtime Database in Flutter - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Realtime Database Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
Realtime UI update with Firebase Realtime Database
You have a Flutter app connected to Firebase Realtime Database. Which code snippet correctly listens to changes in the 'messages' node and updates the UI in real time?
Flutter
final databaseRef = FirebaseDatabase.instance.ref('messages');

// Which widget code below updates UI on data changes?
A
FutureBuilder(
  future: databaseRef.onValue,
  builder: (context, snapshot) {
    if (!snapshot.hasData) return CircularProgressIndicator();
    final data = snapshot.data!.snapshot.value as Map<dynamic, dynamic>?;
    return ListView(
      children: data!.entries.map((e) => Text(e.value.toString())).toList(),
    );
  },
)
B
FutureBuilder(
  future: databaseRef.get(),
  builder: (context, snapshot) {
    if (!snapshot.hasData) return CircularProgressIndicator();
    final data = snapshot.data!.value as Map<dynamic, dynamic>?;
    return ListView(
      children: data!.entries.map((e) => Text(e.value.toString())).toList(),
    );
  },
)
C
StreamBuilder(
  stream: databaseRef.once().asStream(),
  builder: (context, snapshot) {
    if (!snapshot.hasData) return CircularProgressIndicator();
    final data = snapshot.data!.snapshot.value as Map<dynamic, dynamic>?;
    return ListView(
      children: data!.entries.map((e) => Text(e.value.toString())).toList(),
    );
  },
)
D
StreamBuilder(
  stream: databaseRef.onValue,
  builder: (context, snapshot) {
    if (!snapshot.hasData) return CircularProgressIndicator();
    final data = snapshot.data!.snapshot.value as Map<dynamic, dynamic>?;
    return ListView(
      children: data!.entries.map((e) => Text(e.value.toString())).toList(),
    );
  },
)
Attempts:
2 left
💡 Hint
Realtime updates require a stream, not a future.
🧠 Conceptual
intermediate
1:30remaining
Understanding Firebase Realtime Database data structure
In Firebase Realtime Database, which data structure is recommended to store a list of chat messages to avoid issues with data ordering and scalability?
AUse nested maps with user IDs as keys and messages as values.
BUse a list (array) with numeric indexes as keys.
CUse a map with unique push IDs as keys for each message.
DStore all messages in a single string separated by commas.
Attempts:
2 left
💡 Hint
Firebase push IDs help keep data ordered and unique.
lifecycle
advanced
1:30remaining
Managing Firebase Realtime Database listeners lifecycle
You add a listener to a Firebase Realtime Database reference in a Flutter StatefulWidget's initState. What is the best practice to avoid memory leaks?
ANo need to remove listeners; Firebase handles it automatically.
BRemove the listener in the dispose method using cancel() on the StreamSubscription.
CRemove the listener in the build method after every build.
DAdd the listener again in dispose to refresh it.
Attempts:
2 left
💡 Hint
Listeners consume resources and should be cleaned up.
📝 Syntax
advanced
1:00remaining
Correct syntax for writing data to Firebase Realtime Database
Which Flutter code snippet correctly writes a new message {'text': 'Hello'} to the 'messages' node using push()?
Flutter
final databaseRef = FirebaseDatabase.instance.ref('messages');
AdatabaseRef.push().set({'text': 'Hello'});
BdatabaseRef.set({'text': 'Hello'});
CdatabaseRef.push({'text': 'Hello'});
DdatabaseRef.update({'text': 'Hello'});
Attempts:
2 left
💡 Hint
push() creates a new unique child, set() writes data.
🔧 Debug
expert
2:00remaining
Debugging Firebase Realtime Database permission error
Your Flutter app throws a permission denied error when trying to read data from Firebase Realtime Database. What is the most likely cause?
AThe database rules do not allow read access to the requested path.
BThe data does not exist at the requested path.
CThe app is offline and cannot connect to Firebase.
DThe Flutter app version is outdated.
Attempts:
2 left
💡 Hint
Check your Firebase Realtime Database rules in the console.