所以我有一个问题,我从列表中删除了一个项目,这给了我一个超出范围的错误,它告诉我在尝试构建我的名片视图时会发生
在手机屏幕上:
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<Todo> todos = [
Todo("Learn Flutter"),
Todo("Make Todo App List"),
Todo("Search for out of range error"),
];
我的列表的卡片视图:
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),
),
);
}
}
答案 0 :(得分:0)
您可以在下面复制粘贴运行完整代码
您可以将class MyModel(models.Model):
...
def create_zip(self, user, jsondata):
filename = "data" + ".zip"
with gzip.GzipFile(filename, "w") as fout:
fout.write(jsondata.encode("utf-8"))
my_model = MyModel.objects.create(
user=user, animation_file=filename
)
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();