gpt4 book ai didi

android - 当我尝试从列表中删除项目时, flutter ,索引超出范围

转载 作者:行者123 更新时间:2023-12-03 08:40:28 25 4
gpt4 key购买 nike

所以我有一个问题,我从列表中删除了一个项目,它给了我一个超出范围的错误,它告诉我在尝试建立我的名片 View 时发生
在手机屏幕上:

 RangeError (index): Invalid value: Not in range 0..1, inclusive: 2 

在调试控制台上:
════════ Exception caught by widgets library ═══════════════════════════════════
The following RangeError was thrown building ListViewCard-[<'2'>](dirty, state: _ListViewCard#e102b):
RangeError (index): Invalid value: Not in range 0..1, inclusive: 2

The relevant error-causing widget was
ListViewCard-[<'2'>]
package:my_todo_app/main.dart:128
When the exception was thrown, this was the stack
#0 List.[] (dart:core-patch/growable_array.dart:146:60)
#1 _ListViewCard.build
package:my_todo_app/main.dart:64
#2 StatefulElement.build
package:flutter/…/widgets/framework.dart:4619
#3 ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:4502
#4 StatefulElement.performRebuild

我不知道我做错了什么,当我添加项目时,列表会更新得很好,
这是我的代言人:一个简单的 list :
List<Todo> todos = [
Todo("Learn Flutter"),
Todo("Make Todo App List"),
Todo("Search for out of range error"),
];

我的 list 的卡片 View :
class ListViewCard extends StatefulWidget {
final int index;
final Key key;
final List<Todo> todoList;

ListViewCard(this.index, this.key, this.todoList);
@override
_ListViewCard createState() => _ListViewCard();
}


class _ListViewCard extends State<ListViewCard> {
@override
Widget build(BuildContext context) {
return Card(
child: ListTile(
title: Text(widget.todoList[widget.index].text),
trailing: IconButton(
icon: Icon(Icons.delete),
onPressed: () {
setState(() {
todos.removeAt(widget.index);
});
},
),
onTap: () {},
),
);
}
}

主场:
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Todo),
),
body: ReorderableListView(
onReorder: () {},
children: List.generate(todos.length, (index) {
return ListViewCard(index, Key('$index'), todos);
}),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
setState(() {
todos.insert(todos.length, Todo("New Todo"));
});
},
child: Icon(Icons.add),
),
);
}
}

最佳答案

您可以在下面复制粘贴运行完整代码
您可以将callback refresh()传递给ListViewCard,而refresh()将执行setState程式码片段

class _MyHomePageState extends State<MyHomePage> {
...
void refresh() {
setState(() {});
}

class ListViewCard extends StatefulWidget {
final int index;
final Key key;
final List<Todo> todoList;
VoidCallback callback;

ListViewCard(this.index, this.key, this.todoList, this.callback);
...
onPressed: () {
todos.removeAt(widget.index);
widget.callback();

工作演示

enter image description here

完整的代码
import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}

class Todo {
String text;
Todo(this.text);
}

List<Todo> todos = [
Todo("Learn Flutter"),
Todo("Make Todo App List"),
Todo("Search for out of range error"),
];

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
void _onReorder(int oldIndex, int newIndex) {
setState(
() {
if (newIndex > oldIndex) {
newIndex -= 1;
}
final Todo item = todos.removeAt(oldIndex);
todos.insert(newIndex, item);
},
);
}

void refresh() {
setState(() {});
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Todo"),
),
body: ReorderableListView(
onReorder: _onReorder,
children: List.generate(todos.length, (index) {
return ListViewCard(index, Key('$index'), todos, refresh);
}),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
setState(() {
todos.insert(todos.length, Todo("New Todo"));
});
},
child: Icon(Icons.add),
),
);
}
}

class ListViewCard extends StatefulWidget {
final int index;
final Key key;
final List<Todo> todoList;
VoidCallback callback;

ListViewCard(this.index, this.key, this.todoList, this.callback);
@override
_ListViewCard createState() => _ListViewCard();
}

class _ListViewCard extends State<ListViewCard> {
@override
Widget build(BuildContext context) {
return Card(
child: ListTile(
title: Text(widget.todoList[widget.index].text),
trailing: IconButton(
icon: Icon(Icons.delete),
onPressed: () {
todos.removeAt(widget.index);
widget.callback();
/*setState(() {
todos.removeAt(widget.index);
});*/
},
),
onTap: () {},
),
);
}
}

关于android - 当我尝试从列表中删除项目时, flutter ,索引超出范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62011871/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com