Home > Back-end >  Parsing Iterative JSON from Firebase to Flutter
Parsing Iterative JSON from Firebase to Flutter

Time:07-02

I have data saved into Realtime Firebase as an iterative JSON as shown in the picture.

Realtime Firebase data

[
  {
    "name": "Math",
    "subMenu": [
      {
        "name": "Math1",
        "subMenu": [
          {
            "name": "Math 1.1"
          },
          {
            "name": "Math 1.2",
            "subMenu": [
              {
                "name": "Math 1.2.1",
                "subMenu": [
                  {
                    "name": "Math 1.2.1.1"
                  },
                  {
                    "name": "Math 1.2.1.2"
                  }
                ]
              },
              {
                "name": "Math 1.2.2"
              }
            ]
          }
        ]
      },
      {
        "name": "Math2"
      },
      {
        "name": "Math3",
        "subMenu": [
          {
            "name": "Math 1.3.1"
          },
          {
            "name": "Math 1.3.2"
          }
        ]
      }
    ]
  },
  {
    "name": "Marketing",
    "subMenu": [
      {
        "name": "Promotions",
        "subMenu": [
          {
            "name": "Catalog Price Rule"
          },
          {
            "name": "Cart Price Rules"
          }
        ]
      },
      {
        "name": "Communications",
        "subMenu": [
          {
            "name": "Newsletter Subscribers"
          }
        ]
      }
    ]
  }
]

How the JSON look like in Realtime Firebase 'Click the image'

datamodel.dart

class Menu {
  String? name;
  int? font;
  List<Menu>? subMenu = [];

  Menu({this.name, this.subMenu, this.font});

  Menu.fromJson(Map<String, dynamic> json) {
    font = json['font'];
    name = json['name'];

    if (json['subMenu'] != null) {
      json['subMenu'].forEach((v) {
        subMenu?.add(Menu.fromJson(v));
      });
    }
  }
}

My goal is to build a multilevel list view in Flutter that reflexes iterative JSON structure. So, I implemented a method that returns List<Menu>, and then pass it to a Futurebuilder to build a multilevel list View.

The method.

final ref = FirebaseDatabase.instance.ref();

  Future<List<Menu>> firebaseCalls(DatabaseReference ref) async {
    final snapshot = await ref.child('Task').get();
    final jsondata = snapshot.value as Map<String, dynamic>;
    final list = json.decode(jsondata) as List<dynamic>; // Error Location
    return list.map((e) => Menu.fromJson(e)).toList();
  }

and I got the following

The Error error: The argument type 'Map<String, dynamic>' can't be assigned to the parameter type 'String'. (argument_type_not_assignable at [flutter_multilevel_list_from_json] lib\main.dart:28)

tried to change the list type to List<dynamic> but still give me an error.

CodePudding user response:

json.decode() takes a String as input, and you are passing a Map<String,dynamic> into it.

That is your problem, not that you are trying to cast it to a List<dynamic>

CodePudding user response:

May be this will be helpful (jsondata as List).map((e) => Menu.fromJson(e)).toList();

  • Related