Home > Mobile >  ListView infinite loop when parsing data from API response
ListView infinite loop when parsing data from API response

Time:08-15

I'm trying to read data from some mock endpoint. enter image description here

CodePudding user response:

In here builder you should use,itemCount parameter

 ListView.builder(
    itemCount: list.length,
    itemBuilder: (context, index) {
        return Your Widget;
    }),

CodePudding user response:

Create a state variable for future and include itemCount: list.length,

final myFuture  = ConfigurationController.getOpticsTools2();

And use it on

 child: FutureBuilder<ApiCallResponse>(
          future: myFuture  ,
          builder: (context, snapshot) {

CodePudding user response:

I struggled for so long but clearly, the issue was not passing in the itemCount argument into the ListView.builder() method. Also, the outer loop was invalid as now I need to use the actual itemIndex within the builder. Thanks for pointing out the itemCount all! Here's the fixed code and the solution in case anyone needs it later.

@override Widget build(BuildContext context) { final opticsToolsMockResponse = ConfigurationController.getOpticsTools2();

return Scaffold(
  backgroundColor: Colors.black,
  appBar: StandardAppbarWidget(appBarTitle: "some title"),
  body: SizedBox(
    child: FutureBuilder<ApiCallResponse>(
      future: opticsToolsMockResponse,
      builder: (context, snapshot) {
        if (!snapshot.hasData) {
          return Center(
            child: SizedBox(
              width: 50,
              height: 50,
              child: CircularProgressIndicator(
                color: Colors.red,
              ),
            ),
          );
        }

        final gridViewGetToolsOpticsResponse = snapshot.data;

        var toolCategories = getJsonField(
          gridViewGetToolsOpticsResponse.jsonBody,
          r'''$.result.toolList''',
        ).toList();

        return Builder(
          builder: (context) {
            return ListView.builder(
                itemCount: toolCategories.length,
                itemBuilder: (context, itemIndex) {
                  final widgets = <Widget>[];

                  var currentToolCategory = getJsonField(
                    toolCategories[itemIndex],
                    r'''$.category''',
                  );

                  widgets.add(Text(
                    currentToolCategory,
                    style: Colors.white,
                  ));

                  var toolListInCategory = getJsonField(
                    toolCategories[itemIndex],
                    r'''$.tools''',
                  );

                  for (int j = 0; j < toolListInCategory.length; j  ) {
                    var toolDisplayName = getJsonField(
                      toolListInCategory[j],
                      r'''$.displayName''',
                    );

                    widgets.add(Text(toolDisplayName));
                  }

                  return SingleChildScrollView(
                      child: Column(
                    children: widgets,
                  ));
                });
          },
        );
      },
    ),
  ),
);

}

enter image description here

CodePudding user response:

You just forgot to specify the size of the list, you should do it with the itemCount property in the ListView.builder widget

itemCount: list.length,

  • Related