Flutter:未为DataSnapshot类定义forEach方法 [英] Flutter: The method forEach isn't defined for the class DataSnapshot

查看:212
本文介绍了Flutter:未为DataSnapshot类定义forEach方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要迭代Firebase中的 DatabaseReference 中的节点。但据连线, firebase_database中的 DataSnapshot 中没有 forEach 函数库!



我还尝试使用 DataSnapshot 对象> firebase 库(其中具有forEach函数),但出现错误:

  [ dart]参数类型'((DataSnapshot)→List< dynamic>'不能被分配给参数类型'(DataSnapshot)→FutureOr< dynamic>'。 

这是我的代码:

  getAccountsList(){
return firebaseDbService.getAccounts()。once()。then((DataSnapshot snapshot){
var list = [];
快照.forEach((DataSnapshot帐户)=> list.add({
'id':snapshot.key,
'name':snapshot.child('name')。val(),
}));
返回清单;
});
}


解决方案

目前尚不清楚您在尝试什么要在您的代码中执行, child(String path) val()都不在类<$中c $ c> DataSnapshot ,您可以在此处查看:





这意味着您也不能使用 forEach()




让我们假设您拥有此数据库,并且想要获取名称

 用户
randomId
名称:John
randomId
名称:Peter

您需要执行以下操作:

  _db = FirebaseDatabase.instance.reference()。child( user); 
_db.once()。then((DataSnapshot快照){
Map< dynamic,dynamic&value; values = snapshot.value;
print(values.toString());
values.forEach((k,v){
print(k);
print(v [ name]);
});
});

此处指向节点 users 的参考点,由于 snapshot.value 的类型为 Map< dynamic,dynamic> ,因此您可以执行地图<动态,动态> values = snapshot.value;



然后在地图中使用< a href = https://api.dartlang.org/stable/2.0.0/dart-collection/LinkedHashMap-class.html rel = noreferrer> forEach() 以获取键和值,您将获得以下输出:





此行 I / flutter(2799):{-LItvfNi19tptjdCbHc3:{name:peter},-LItvfNi19tptjdCbHc1:{name:john}} print(values.toString());



以下两行的输出:

  I / flutter(2799):-LItvfNi19tptjdCbHc3 
I / flutter(2799):-LItvfNi19tptjdCbHc1

print(k)的输出;



其他两行是 print(v [ name]);


$的输出b $ b

添加名称放入列表中,在 forEach()内执行以下操作:

  list.add(v [ name]); 
print(list);


I need to iterate a node in a DatabaseReference in firebase. But it is wired that there is no forEach function in DataSnapshot that is in firebase_database library!

I also tried to use DataSnapshot object that is in firebase library (that has a forEach function in it) but I got a error:

[dart] The argument type '(DataSnapshot) → List<dynamic>' can't be assigned to the parameter type '(DataSnapshot) → FutureOr<dynamic>'.

And here is my code:

getAccountsList() {
  return firebaseDbService.getAccounts().once().then((DataSnapshot snapshot) {
    var list = [];
    snapshot.forEach((DataSnapshot account) => list.add({
      'id': snapshot.key,
      'name': snapshot.child('name').val(),
    }));
    return list;
  });
}

解决方案

It's unclear what you are trying to do in your code, both child(String path) and val() do not exist in the class DataSnapshot, you can check here:

https://github.com/flutter/plugins/blob/master/packages/firebase_database/lib/src/event.dart#L27

Also you cannot loop like this:

for( var values in snapshot.value){
 print("Connected to second database and read ${values}");
}

since you will get the following error:

which means also you cannot use forEach() on the snapshot to iterate.


Let's say you have this database, and you want to get the names:

user
  randomId
     name: John
  randomId
     name: Peter

You need to do the following:

_db=FirebaseDatabase.instance.reference().child("user");
_db.once().then((DataSnapshot snapshot){
   Map<dynamic, dynamic> values=snapshot.value;
   print(values.toString());
     values.forEach((k,v) {
        print(k);
        print(v["name"]);
     });
 });

Here the reference points toward the node users, since snapshot.value is of type Map<dynamic,dynamic> then you are able to do this Map<dynamic, dynamic> values=snapshot.value;.

Then you loop inside the map using forEach() to be able to get the keys and values, you will get the following output:

This line I/flutter ( 2799): {-LItvfNi19tptjdCbHc3: {name: peter}, -LItvfNi19tptjdCbHc1: {name: john}} is the output of print(values.toString());

Both the following lines:

I/flutter ( 2799): -LItvfNi19tptjdCbHc3
I/flutter ( 2799): -LItvfNi19tptjdCbHc1

are the output of print(k);

The other two lines are the output of print(v["name"]);

To add the names into a list do the following inside the forEach():

list.add(v["name"]);
    print(list);

这篇关于Flutter:未为DataSnapshot类定义forEach方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆