Flutter - Firebase RTDB read issue for node at index 0, 1 and 2

我的未来我决定 提交于 2020-07-23 06:42:38

问题


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",

}]

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());

   }
}

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:

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?


回答1:


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 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.

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.



来源:https://stackoverflow.com/questions/62107352/flutter-firebase-rtdb-read-issue-for-node-at-index-0-1-and-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!