从firebase数据库中读取项目列表

从firebase数据库中读取项目列表,firebase,dictionary,flutter,firebase-realtime-database,Firebase,Dictionary,Flutter,Firebase Realtime Database,我正在尝试从此数据库中生成项目列表: 但我得到了一个错误: _TypeError (type 'List<dynamic>' is not a subtype of type 'List<DataSnapshot>') NoSuchMethodError (NoSuchMethodError: The method 'add' was called on null. Receiver: null Tried calling: add(Instance of 'Item

我正在尝试从此数据库中生成项目列表:

但我得到了一个错误:

_TypeError (type 'List<dynamic>' is not a subtype of type 'List<DataSnapshot>')
NoSuchMethodError (NoSuchMethodError: The method 'add' was called on null.
Receiver: null
Tried calling: add(Instance of 'Item'))

在FlatterFire中,无法将子节点作为
DataSnapshot
对象列表获取

我得到的最接近的结果是:

currentRoundListener = dbRoot.child('rounds/$currentRoundKey').once.then((snapshot) {
  currentRound = List<String>.from(snapshot.value as List<dynamic>);
});
currentRoundListener=dbRoot.child('rounds/$currentRoundKey')。一次。然后((快照){
currentRound=List.from(snapshot.value作为列表);
});
您可以尝试以下方法:

List<DataSnapshot> result = List<DataSnashot>.from(snap2.value as List<dynamic>);
List result=List.from(snap2.value作为List);
但快照下的值更有可能仅作为映射提供:

Map<String, dynamic> result = Map<String, dynamic>.from(snap2.value as Map<dynamic, dynamic>);
Map result=Map.from(snap2.value作为Map);


如果需要维护子节点的顺序,请查看此处:

我已使用以下实现解决了此问题:

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();
  }
} 
getItems()异步{
弦线;
列出结果;
等待_refdata.child(“ref”).once()。然后((value)=>_refOnline=value.value);
如果(数据参考!=\u重新联机){
等待_refdata.child(“值/值”).once(),然后((DataSnapshot snap2){
结果=snap2.0值;
int_i=1;
result.forEach((值){
if(值!=null){
项目_a=项目(
_i、 toString(),
值[“nome”],
值[“preco”].toDouble(),
值[“预成本”]??“,
值[“tipo”],
值[“disponivel”],
值[“描述”],
价值[“卡路里]);
列表a.添加(_a);
_i+=1;
}
});
savepref(“ref_key”,refOnline);
});
}否则{
列表a=[oi,oi2,oi3,oi4,oi5,oi6,oi7,oi8,oi9,oi10,oi11,oi12];
//readIt();
}
} 
lista定义出现问题,阻止它在lista.add函数中接收新项

我现在将lista定义为:

List<Item> lista = [];
List lista=[];
通过上述功能,现在一切正常


弗兰克·范·帕夫伦

对于List.from选项,im收到此错误:发生异常_TypeError(类型“\u InternalLinkedHashMap”不是类型“DataSnapshot”的子类型)并且对于映射结果来说,\u CastError(类型“List”不是类型转换中类型“Map”的子类型)是的,第一个错误是预期的,因为客户端只是不公开子
DataSnapshot
s。第二个错误似乎是因为我的代码中有一个剩余的复制/粘贴,这与您的代码不同。我更新了我的答案。最后一行应该适合你。在我有数字按键的地方,我自己用它来绘制地图,但仍然无法用地图来解决问题。在没有钥匙的情况下,我在下面的回答中成功了。
Map<String, dynamic> result = Map<String, dynamic>.from(snap2.value as Map<dynamic, dynamic>);
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();
  }
} 
List<Item> lista = [];