Reputation: 342
I'm trying to build a list of items from this database:
but i'm getting this error:
_TypeError (type 'List<dynamic>' is not a subtype of type 'List<DataSnapshot>')
i can get the item values just using snap2.value but i need to get the element key because it's my item id, to build my list.
import 'package:firebase_database/firebase_database.dart';
final DatabaseReference _refdata =
FirebaseDatabase.instance.reference().child('host');
getItems() async {
String _refOnline;
await _refdata.child("ref").once().then((value) => _refOnline = value.value);
if (dataRef != _refOnline) {
await _refdata.child("values/valores").once().then((DataSnapshot snap2) {
List<DataSnapshot> result = snap2.value;
lista = result
.map((element) => Item(
element.key,
element.value["nome"],
element.value["preco"].toDouble(),
element.value["precoantes"] ?? "",
element.value["tipo"],
element.value["disponivel"],
element.value["descricao"],
element.value["calorias"]))
.toList();
});
edit:
with this change im able to return values:
List<Map<dynamic, dynamic>> result =
List<Map<dynamic, dynamic>>.from(snap2.value);
result.forEach((element) {
if (element != null) {
print(element);
}
});
but i cant return the keys (1,2,3,4,5)
and it's the same as doing this(suposing query ordered by keys):
List<dynamic> result = snap2.value;
int _i = 1;
result.forEach((value) {
if (value != null) {
lista.add(Item(
_i.toString(),
value["nome"],
value["preco"].toDouble(),
value["precoantes"] ?? "",
value["tipo"],
value["disponivel"],
value["descricao"],
value["calorias"]));
_i += 1;
print(value["nome"]);
print(value);
print(lista.length);
}
and now im getting this error:
NoSuchMethodError (NoSuchMethodError: The method 'add' was called on null.
Receiver: null
Tried calling: add(Instance of 'Item'))
Upvotes: 2
Views: 5186
Reputation: 342
I've managed to solve it using this implementation:
getItems() async {
String _refOnline;
List<dynamic> result;
await _refdata.child("ref").once().then((value) => _refOnline = value.value);
if (dataRef != _refOnline) {
await _refdata.child("values/valores").once().then((DataSnapshot snap2) {
result = snap2.value;
int _i = 1;
result.forEach((value) {
if (value != null) {
Item _a = Item(
_i.toString(),
value["nome"],
value["preco"].toDouble(),
value["precoantes"] ?? "",
value["tipo"],
value["disponivel"],
value["descricao"],
value["calorias"]);
lista.add(_a);
_i += 1;
}
});
savepref("ref_key", _refOnline);
});
} else {
lista = [oi, oi2, oi3, oi4, oi5, oi6, oi7, oi8, oi9, oi10, oi11, oi12];
//readIt();
}
}
there was a problem on lista definition blocking it to receive new items in lista.add function.
I've now defined lista as:
List<Item> lista = [];
And with the above function, everything now works.
thx Frank van Puffelen !
Upvotes: 0
Reputation: 598901
There is no way in FlutterFire to get the child nodes as a list of DataSnapshot
objects.
The closest I got was:
currentRoundListener = dbRoot.child('rounds/$currentRoundKey').once.then((snapshot) {
currentRound = List<String>.from(snapshot.value as List<dynamic>);
});
You could give this a try with:
List<DataSnapshot> result = List<DataSnashot>.from(snap2.value as List<dynamic>);
But more likely the values under the snapshot will only be available as a map:
Map<String, dynamic> result = Map<String, dynamic>.from(snap2.value as Map<dynamic, dynamic>);
If you need to maintain the order of the child nodes, have a look here: Flutter: Firebase Real-Time database orderByChild has no impact on query result and here: Flutter Firebase Database wrong timestamp order
Upvotes: 3