正文可能会正常完成,导致返回"null",但返回类型"Widget"可能是不可为空的类型



我是Flutter的初学者,我的代码给我带来了错误。

我正在构建一个简单的待办事项列表应用程序,并使用待办事项列表的列表生成器来构建

在使用项目ListView.builder时,错误显示我无法解决此问题,请帮助我,提前感谢您帮助

class TodoListState extends State<todoList> {
final List<String> _todoItems = [];
// This will be called each time the + button is pressed
void _addTodoItem() {
// Putting our code inside "setState" tells the app that our state has changed, and
// it will automatically re-render the list
setState(() {
int index = _todoItems.length;
_todoItems.add('Item ' + index.toString());
});
}
// Build the whole list of todo items
Widget _buildTodoList() {
return  ListView.builder(
itemBuilder: (context, index){ 
// I get =>  The body might complete normally, causing 'null' to be returned, but the return type, 'Widget', is a potentially non-nullable type
if(index < _todoItems.length) {
return _buildTodoItem(_todoItems[index]);
}
},
);
}
// Build a single todo item
Widget _buildTodoItem(String todoText) {
return  ListTile(
title: Text(todoText)
);
}
@override
Widget build(BuildContext context) {
return  Scaffold(
appBar:  AppBar(
title: const Text('Todo List')
),
body: _buildTodoList(),
floatingActionButton:  FloatingActionButton(
onPressed: _addTodoItem,
tooltip: 'Add task',
child: const Icon(Icons.add)
),
);
}
} 

改为执行以下操作:

Widget _buildTodoList() {
return  ListView.builder(
lenght : _todoItems.length,
itemBuilder: (context, index){ 
return _buildTodoItem(_todoItems[index]);
},
);
}

相关内容

最新更新