Home > Software design >  Not Showing Data from Api In DataTable
Not Showing Data from Api In DataTable

Time:11-30

hi i have json like this:

"data": {
        "list_item": [
            {
                "item": "1",
                "item_date": "1669189813143566825",
                "item_id": "0",
                "item_info": {},"
                "item_status":"on",
                    }]}

when parse json and show in data table show message table null and my class:

class TableSamleNew extends StatefulWidget {
  const TableSamleNew({Key? key}) : super(key: key);

  @override
  State<TableSamleNew> createState() => _TableSamleNewState();
}

class _TableSamleNewState extends State<TableSamleNew> {
  final getListController = Get.put(GetListController());

  @override
  Widget build(BuildContext context) {
    List<ListItem>? listItem;
    listItem=getListController.getListClient!.data!.listItem;

    return Scaffold(
      body: DataTable(columns: [
        DataColumn(label: Text("1")),
        DataColumn(label: Text("1")),
        DataColumn(label: Text("1")),
        DataColumn(label: Text("1")),
        DataColumn(label: Text("1"))
      ],rows: listItem!.map<DataRow>((e) => DataRow(cells: [
        DataCell(Text(e.itemId.toString())),
        DataCell(Text(e.itemId.toString())),
        DataCell(Text(e.itemId.toString())),
        DataCell(Text(e.itemId.toString())),
        DataCell(Text(e.itemId.toString())),
      ])).toList()),
    );
  }
}

why showing me table is Null? field in table 5 and in json all field is 5

my Controller:

class GetListController extends GetxController {
  GetListClient? getListClient;
  bool isLoading = false;
  var input = Get.arguments;

  //When Call Side Menu From Side Menu Screen On Init Is Called By First
  @override
  void onInit() async {
    super.onInit();
    updateIsLoading(true);
    final apiHelper = ApiHelper();
    getListClient=await apiHelper.PortalGetList(input);
    updateIsLoading(false);

  }
  void updateIsLoading(bool currentStatus) {
    isLoading = currentStatus;
    update();
  }
}

when print portal get list Showing All of Data in Console.

CodePudding user response:

You need to define these variable out of build methode:

late List<ListItem>? listItem =getListController.getListClient!.data!.listItem;

every time the widget rebuild you variable redefine and became empty.

CodePudding user response:

class TableSamleNew extends StatefulWidget {
  const TableSamleNew({Key? key}) : super(key: key);

  @override
  State<TableSamleNew> createState() => _TableSamleNewState();
}

class _TableSamleNewState extends State<TableSamleNew> {
  final getListController = Get.put(GetListController());
ValueNotifier<List<ListItem>> listItemNotifier = ValueNotifier([]);

  @override
  Widget build(BuildContext context) {
    listItemNotifier.value=getListController.getListClient!.data!.listItem;

    return Scaffold(
       body: ValueListenableBuilder(
       valueListenable: listItemNotifier, 
       builder:(context,List<ListItem> items, child) =>
          DataTable(columns: [
          DataColumn(label: Text("1")),
          DataColumn(label: Text("1")),
          DataColumn(label: Text("1")),
          DataColumn(label: Text("1")),
          DataColumn(label: Text("1"))
       ],rows: items.map<DataRow>((e) => DataRow(cells: [
          DataCell(Text(e.itemId.toString())),
          DataCell(Text(e.itemId.toString())),
          DataCell(Text(e.itemId.toString())),
          DataCell(Text(e.itemId.toString())),
          DataCell(Text(e.itemId.toString())),
          ])).toList()),
       );
    );
  }
}

try to use ValueNotifier

  • Related