Flutter-索引0、1和2的节点的Firebase RTDB读取问题 [英] Flutter - Firebase RTDB read issue for node at index 0, 1 and 2

查看:82
本文介绍了Flutter-索引0、1和2的节点的Firebase RTDB读取问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Flutter代码中,我有一个下拉列表链接到汽车列表。一旦选择了汽车,我想检索与汽车相关的品牌。但是由于某些原因,用于读取Firebase RTDB的代码对于索引0,索引1和索引2及更高版本必须有所不同。我从RTDB中获取的示例树如下:

In my Flutter code, I have a dropdown list linked to a list of cars. Once a car is selected I want to retrieve the brand associated to the car. But for some reason the code used to read the firebase RTDB has to be different for Index 0, index 1 and Index 2 and above. My sample tree taken as is from my RTDB is as follows:

[ {
  "Brand" : "Alfa Romeo",
  "Car" : "Alfa Romeo Guilia 2.9 V6 BiTurbo Quadrifoglioli Automatic Petrol Sedan RWD",

}, {
  "Brand" : "Alfa Romeo",
  "Car" : "Alfa Romeo Guilia 2.0T Standard Automatic Petrol Sedan RWD",

}, {
 "Brand" : "Alfa Romeo",
 "Car" : "Alfa Romeo Guilia 2.0T Super Automatic Petrol Sedan RWD",

}, {
  "Brand" : "Alfa Romeo",
  "Car" : "Alfa Romeo Stelvio 2.0 Turbo Super Automatic Petrol SUV AWD",

}, {
  "Brand" : "Alfa Romeo",
  "Car" : "Alfa Romeo Stelvio 2.0 Turbo First Edition Automatic Petrol SUV AWD",

}, {
  "Brand" : "Alfa Romeo",
  "Car" : "Alfa Romeo 4C 1750Tbi N/A TCT Petrol Coupe RWD",

}]

上面的父项是 CarList2

The parent of the above is `CarList2'

在我的颤动代码中,我必须使用以下内容来检索品牌:

In my flutter code I have to use the following to retrieve the brand:

final dbRef = FirebaseDatabase.instance.reference().child('CarList2');

void CreateNewDeal(String selectedcar) async{

await dbRef.orderByChild('Car').equalTo(selectedcar).once().then((DataSnapshot snap) {

    print('length - ${snap.value.length}');

    List<dynamic> brand = [];

// Code for Index 0 and index 1

    snap.value.forEach((v1) {

      brand.add(v1);
      if(snap.value.length == 1 && brand[0]['Brand'] != null){

        selectedbrand = brand[0]['Brand'];
        print('brand index0 - ${brand[0]['Brand']}');

      }

      else if(snap.value.length == 2){

        print('brand index 1 - ${snap.value[1]['Brand']}');
      }

    });

/**
        following code gets the brand for index 1--> 
     */

    Map<dynamic,dynamic> values = snap.value;

    values.forEach((key, v2) {

      selectedbrand = v2['Brand'];
      print('Brand index 2>:(${v2['Brand']})');

    });

  });

}catch(e){
  print(e.toString());

   }
}

在上面,我无法运行如果选择索引1的汽车,则索引0和索引2与索引1的时间相同,因为代码将在索引0或2上返回空值,然后再从索引1的代码中删除。

In the above, I cannot run the index 0 and index 2 the same time as index 1 if the index 1 car is selected because the code will return a null on the index 0 or 2 and then never makes it to the code for index 1.

我还为孩子获得了可变长度的快照,如下所示:

I also get variable lengths of the snapshot for the children as follows:

索引0 =长度1
索引1-长度2
索引2及以上=长度1

Index 0 = length 1 Index 1 - length 2 Index 2 and above = length 1

我做错了还是要以不同的方式阅读?

Am I doing something wrong or am I meant to be reading this in a different way?

推荐答案

对于寻求类似解决方案的任何人,请注意他在评论中提供给我的Frank van Puffelen的回答。阅读文章,但总而言之,请远离编号索引。

For anyone looking for a similar solution, take note of Frank van Puffelen's answer he provided me in the comments. read the article, but in short, stay clear of numbered indexes.

弗兰克(Frank)为我提供了实质上重新构造树的方向,这很容易做到。使用firebase导出,使用Json到CSV转换器(可以在线找到-我使用 https: //www.convertcsv.com/csv-to-json.htm )。一旦为CSV格式,请在第一列中添加一个 key 列。添加任何顺序字符串作为键。然后使用在线CSV到Json转换器再次进行转换。我使用了这个( https://csvjson.com/csv2json ),因为您想要的是创建新的树作为哈希图(至少在我的实例中),您不希望在键和子节点之间再次创建编号索引,而另一个网站最终会这样做。

Frank gave me the direction to essentially re-structure my tree which is quite easy to do. export using firebase, use a Json to CSV converter (can be found online - i used https://www.convertcsv.com/csv-to-json.htm). once in CSV format, add a key column as the first column. add any sequential string as the keys. Then convert again using an online CSV to Json converter. I used this one (https://csvjson.com/csv2json) because what you want is to create the new tree as a hashmap (at least in my instance) you do not want a numbered index created between the key and the children again which the other website ended up doing.

这样做,您现在可以检索所有节点的子节点,而不会出现空错误,而我在索引0,1和2之间得到错误。

By doing this you now able to retrieve the child for all nodes without the null error i was getting between Index 0,1 and 2.

注意:这是一个特定的问题我是因为我的列表是静态的,并且是在应用程序外部创建的,并且仅偶尔更新一次。如果您是通过该应用创建列表的,则应该不会出现这样的错误。

NOTE: this was a specific issue for me because my list is static and gets created outside of the app and only updated once in a while. if you creating your list via the app then you should not be getting an error like this.

这篇关于Flutter-索引0、1和2的节点的Firebase RTDB读取问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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