这是我的代码
import 'package:flutter/material.dart';
void main() => runApp(TodoList());
class TodoList extends StatefulWidget {
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return _TodoListState();
}
}
var TaskTextField;
List taskTextList = [];
var TaskIsImportant = false;
class _TodoListState extends State<TodoList> {
@override
Widget build(BuildContext context) {
// TODO: implement build
return MaterialApp(
title: "To-Do List V2",
home: Scaffold(
appBar: AppBar(
title: Text("To-Do List V2"),
),
body: Flex(
direction: Axis.vertical,
children: <Widget>[
Flexible(
fit: FlexFit.tight,
child: Column(children: <Widget>[
Container(
child: TextField(
decoration: InputDecoration(
hintText: "Enter title of task to be added"),
onChanged: (taskTextField) {
setState(() {
TaskTextField = taskTextField;
print(TaskTextField);
});
},
),
margin: EdgeInsets.all(16.0),
),
CheckboxListTile(
title: Text("Important"),
activeColor: Colors.blue,
value: TaskIsImportant,
onChanged: (val) {
setState(() {
TaskIsImportant = !TaskIsImportant;
print(TaskIsImportant);
});
},
),
Text(
"Tip: Tap on your task to remove it",
style: TextStyle(fontSize: 15.0),
),
Container(
margin: EdgeInsetsDirectional.fromSTEB(117, 10, 117, 5),
child: Row(
children: <Widget>[
RaisedButton(
child: Icon(Icons.add),
onPressed: () {
setState(() {
if (TaskIsImportant) {
taskTextList.add("❗️$TaskTextField");
//taskTitleList.add(TaskDetailField);
} else {
taskTextList.add(TaskTextField);
//taskTitleList.add(TaskDetailField);
}
});
},
),
RaisedButton(
child: Icon(Icons.done_all),
onPressed: () {
setState(() {
taskTextList = [];
});
},
),
],
),
),
Expanded(
child: ListView.builder(
itemBuilder: (context, index) {
var title = taskTextList[index];
padding:
EdgeInsets.all(16.0);
return Dismissible(
key: Key(title[index]),
onDismissed: (direction) {
setState(() {
taskTextList.remove(title);
});
},
child: ListTile(
title: Text(
title,
style: TextStyle(fontSize: 20.0),
),
),
);
},
itemCount: taskTextList.length,
),
)
]),
),
],
)));
}
}
当我在模拟器中创建四个listTiles时,出现以下两个错误
flutter: ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
flutter: The following RangeError was thrown building:
flutter: RangeError (index): Invalid value: Not in range 0..3, inclusive: 4
我希望有人可以告诉我我的代码有什么问题。我正在Flutter版本1.3.10上运行。预先感谢。
对于需要更多代码的人,这也是main.dart的完整代码。希望这可以帮助您找到我的错误
答案 0 :(得分:0)
您正在使用title[index]
,但将title中的值另存为title = taskTextList[index]
,而不是数组。因此,您遇到范围错误。