2

I am trying to parse json response data, in this response there are nested array list and i want to display table_id : 39 and on next line or under table_id : 39 i want to display table_id : 40. Now problem is when it completes on table_id : 39 then it should go to table_id : 40 and display but instead it shows 0floor is null. Can anyone help me in which step i am doing wrong.

JSON RESPONSE

{
    "message": "floors returned",
    "floorData": [
        {
            "0floor": [
                {
                    "table_id": "39"
                }
            ]
        },
        {
            "1floor": [
                {
                    "table_id": "40"
                }
            ]
        }
    ]
}

Model

class TablesFloorData0floor {

  String? tableId;

  TablesFloorData0floor({
    this.tableId,
  });
  TablesFloorData0floor.fromJson(Map<String, dynamic> json) {
    tableId = json['table_id']?.toString();
  }
  Map<String, dynamic> toJson() {
    final data = <String, dynamic>{};
    data['table_id'] = tableId;
    return data;
  }
}

class TablesFloorData {

  List<TablesFloorData0floor?>? the0floor;

  TablesFloorData({
    this.the0floor,
  });
  TablesFloorData.fromJson(Map<String, dynamic> json) {
    if (json['0floor'] != null) {
      final v = json['0floor'];
      final arr0 = <TablesFloorData0floor>[];
      v.forEach((v) {
        arr0.add(TablesFloorData0floor.fromJson(v));
      });
      the0floor = arr0;
    }
  }
  Map<String, dynamic> toJson() {
    final data = <String, dynamic>{};
    if (the0floor != null) {
      final v = the0floor;
      final arr0 = [];
      v!.forEach((v) {
        arr0.add(v!.toJson());
      });
      data['0floor'] = arr0;
    }
    return data;
  }
}

class Tables {
  String? message;
  List<TablesFloorData?>? floorData;

  Tables({
    this.message,
    this.floorData,
  });
  Tables.fromJson(Map<String, dynamic> json) {
    message = json['message']?.toString();
    if (json['floorData'] != null) {
      final v = json['floorData'];
      final arr0 = <TablesFloorData>[];
      v.forEach((v) {
        arr0.add(TablesFloorData.fromJson(v));
      });
      floorData = arr0;
    }
  }
  Map<String, dynamic> toJson() {
    final data = <String, dynamic>{};
    data['message'] = message;
    if (floorData != null) {
      final v = floorData;
      final arr0 = [];
      v!.forEach((v) {
        arr0.add(v!.toJson());
      });
      data['floorData'] = arr0;
    }
    return data;
  }
}

home.dart

  Widget showTable(
      List<TablesFloorData> data, StateSetter setStateBT, String lang) {
    return Container(
        child: ListView.builder(
            shrinkWrap: true,
            itemCount: data.length,
            itemBuilder: (BuildContext context, int index) {
              return Column(
                children: [
                  Text('test'),
                  ListView.builder(
                      shrinkWrap: true,
                      itemCount: data[index].the0floor!.length,
                      itemBuilder: (BuildContext context, int ind) {
                        return Text(data[index].the0floor![index]!.tableId!);
                      })
                ],
              );
            }));
  }

1 Answer 1

3

Try this.. Create one floor class and use a key parameter to parse the floor level.

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: Home(),
    );
  }
}

class Home extends StatelessWidget {
  const Home({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    var data = (json['floorData'] as List<Map<String, dynamic>>)
        .map((f) => TablesFloorData.fromJson(f))
        .toList();
    return Scaffold(
      body: Center(
        child: ListView.builder(
          shrinkWrap: true,
          itemCount: data.length,
          itemBuilder: (BuildContext context, int index) {
            return Column(
              children: [
                Text('test'),
                ListView.builder(
                    shrinkWrap: true,
                    itemCount: data[index].tables.length,
                    itemBuilder: (BuildContext context, int ind) {
                      return Text(
                        data[index].tables[ind].id,
                        textAlign: TextAlign.center,
                      );
                    })
              ],
            );
          },
        ),
      ),
    );
  }
}

class TablesFloorData {
  final List<TableDate> tables;
  final int floor;

  TablesFloorData({
    required this.floor,
    required this.tables,
  });
  factory TablesFloorData.fromJson(Map<String, dynamic> json) =>
      TablesFloorData(
          floor: int.parse(json.keys.first.replaceAll('floor', '')),
          tables: json.values.first.map<TableDate>((tableJson) {
            return TableDate.fromJson(tableJson);
          }).toList());

  Map<String, dynamic> toJson() {
    return {'${floor}floor': tables.map((t) => t.toJson())};
  }
}

class TableDate {
  String id;

  TableDate({
    required this.id,
  });

  factory TableDate.fromJson(Map<String, dynamic> json) => TableDate(
        id: json['table_id'] as String,
      );

  Map<String, dynamic> toJson() {
    return <String, dynamic>{
      'table_id': id,
    };
  }
}

var json = {
  "message": "floors returned",
  "floorData": [
    {
      "0floor": [
        {"table_id": "39"}
      ]
    },
    {
      "1floor": [
        {"table_id": "40"}
      ]
    }
  ]
};
Sign up to request clarification or add additional context in comments.

2 Comments

Instead of showing "test" how i show "0floor" & "1floor" ?
Text('${data[index].floor}floor'),

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.