Home > Software engineering >  Attempting to run a simple widget inside my program but receive the "body_might_complete_normal
Attempting to run a simple widget inside my program but receive the "body_might_complete_normal

Time:07-25

  // Build the whole list of todo items
  Widget _buildTodoList() {
    return new ListView.builder(
      itemBuilder: (context, index) {
        // itemBuilder will be automatically be called as many times as it takes for the
        // list to fill up its available space, which is most likely more than the
        // number of todo items we have. So, we need to check the index is OK.
        if (index < _todoItems.length) {
          return _buildTodoItem(_todoItems[index], index);
        }
      },
    );
  }

Attempting to run a simple widget inside my program but receive the

"body_might_complete_normally" error.

How can I correct this?

CodePudding user response:

itemBuilder only return on (index < _todoItems.length), you need to make sure return widget on every case.

Widget _buildTodoList() {
  return   ListView.builder(
    itemCount:_todoItems.length, // your list length
    itemBuilder: (context, index) {
      if (index < _todoItems.length) {
        return _buildTodoItem(_todoItems[index], index);
      }
      return Text("default case");// if above condtions dont meet, return this
    },
  );
}
  • Related