I am trying to read a list of objects from Realtime database in Firebase with no success so far.
I am following the official tutorial from Firecasts on the following link: https://www.youtube.com/watch?v=sXBJZD0fBa4
Here is how the database looks like:

Here is the code I have written:
The following is the class I have created for the objects I will be reading from database:
class TestData {
final String name;
final String surname;
final String age;
TestData({
required this.name,
required this.surname,
required this.age,
});
factory TestData.fromRTDB(Map<dynamic, dynamic> data) {
return TestData(
name: data["Name"],
surname: data["Surname"],
age: data["Age"],
);
}
}
Here is how I try to read it from the database:
class TestDataGetter {
final _db = FirebaseDatabase.instance.ref();
Stream<List<TestData>> getTestDataStream() {
final csPostprocessedStream = _db.child("test_data/").onValue;
var streamToPublish = csPostprocessedStream.map((event) {
final testDataMap = Map<dynamic, dynamic>.from(
event.snapshot.value as Map<dynamic, dynamic>);
final testDataList = testDataMap.entries.map((element) {
return TestData.fromRTDB(Map<dynamic, dynamic>.from(element.value));
}).toList();
return testDataList;
});
return streamToPublish;
}
}
And here is the screen where I would like to show the data:
class TestDataScreen extends StatelessWidget {
const TestDataScreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
StreamBuilder(
stream: TestDataGetter().getTestDataStream(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
print("Waiting");
return const CircularProgressIndicator(color: Colors.white);
} else if (snapshot.hasError) {
print("Error occured...");
return const CircularProgressIndicator(color: Colors.white);
} else {
final testDataList = snapshot.data as List<TestData>;
return Text(
testDataList[0].name + " / " + testDataList[1].name,
style: Theme.of(context).textTheme.headline3,
textAlign: TextAlign.center);
}
}),
],
),
);
}
}
But I can never see the data on the screen. The spinning wheel is the only thing I see and on the console I see the print out as "Waiting" (as I print out this text in the code above).
It gets stuck in if (!snapshot.hasData).
I am clueless after spending hours on this.