【问题标题】:Get all values from Firebase Database without knowing some child names在不知道某些子名称的情况下从 Firebase 数据库中获取所有值
【发布时间】:2018-10-26 18:18:43
【问题描述】:

我试图在不知道父子 keys 的情况下获取一些子 values,因为它们是由用户或 firebase 生成的 UUID。

我的firebase结构是这样的:

{
  AllUsers: {

      76f4bd92-1fcd-4b4d-86c5-66b7e2822def: { // uuid generated by user

            Users Username: {

                -LCdF2QJ5c46lRnndtmY: { // uuid generated by firebase
                    count: 1,
                    dateTime: 1526479204163,
                    name: "picture1",
                    url: "https://firebasestorage.googleapis.com/",
                },
                -LCdF2QJ5c46lRnndtmQ: {
                    count: 2,
                    dateTime: 1526479204163,
                    name: "picture2",
                    url: "https://firebasestorage.googleapis.com/",
                }
            }
      }
  }
}

我试图让所有用户生成uuidusernames,以及按count 排序时他们的第一张图片的url,以便我可以将它们保存到如下结构:

class GalleryFolderView {
  String uuid;
  String name;
  String firstImageUrl;

  GalleryFolderView(this.uuid, this.name, this.firstImageUrl);
}

我的代码已经做到了这一点,但无法深入了解结构:

  reference = FirebaseDatabase.instance.reference().child("AllUsers");
  reference.onValue.listen((Event event) {
    var key = event.snapshot.value.keys.toString();
    print(key); => // prints out (76f4bd92-1fcd-4b4d-86c5-66b7e2822def) why the brackets? 
  });

任何帮助将不胜感激,谢谢。

【问题讨论】:

    标签: firebase firebase-realtime-database dart flutter


    【解决方案1】:

    这应该做你想做的:

     reference.onValue.listen((Event event) {
        var value = event.snapshot.value;
        var uuids = value.keys;
        for(var uuid in uuids) {
          var userNames = value[uuid].keys;
          for(var userName in userNames) {
            var images = value[uuid][userName].values;
            var image = images.firstWhere((img) => img[count]== 1);
            print('uuid: $uuid, userName: $userName, url: $image);
          }
        } 
      });
    

    【讨论】:

    • 这太棒了,谢谢。如果最低的数字可以是任何东西,我如何找到最低的img[count]
    • import 'dart:math' as math; var lowestId = images.map((img) => img[count]).reduce((a, b) => math.min(a, b)); `var image = images.firstWhere((img) => img[count]== minimumId);
    • 谢谢,我在math.min 上遇到错误,说Could not infer type parameter T. Tried to infer Dynamic for T which doesnt work 消息持续时间更长
    • 请尝试math.min<int>(a, b)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-10
    • 2022-10-08
    • 1970-01-01
    • 2018-01-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多