英文:
Remove data from list when checkbox in unchecked
问题
I used this command widgets.removeAt(data[index]['title']);
but when I tried to uncheck its checkbox not all titles of checkboxes are removed.
在这个命令中使用了widgets.removeAt(data[index]['title'])
,但是当我尝试取消选中复选框时,并没有移除所有复选框的标题。
英文:
I used this command widgets.removeAt(data[index]['title']);
but when I tried to uncheck its checkbox not all titles of checkboxs are removed.
class _MyHomePageState extends State<MyHomePage> {
List<Widget> widgets = [];
List<Map<String, dynamic>> data = [
{'title': 'checkbox1', 'value': false},
{'title': 'checkbox2', 'value': false},
{'title': 'checkbox3', 'value': false}
];
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Column(
children: [
Column(children: widgets),
ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
itemCount: data.length,
itemBuilder: (context, index) {
return CheckboxListTile(
title: Text(data[index]['title']),
value: data[index]['value'],
onChanged: (value) {
setState(() {
// bool check=data[index]['value'] = value!;
if (data[index]['value'] = value!) {
widgets.add(Text(data[index]['title'].toString()));
print(data[index]['title'].toString());
} else {
widgets.removeAt(index);
}
});
});
},
),
],
),
); }
}
Screenshot here
In that picture checkbox title not removed;
in the console I got this error:
> Value not in range: 2
答案1
得分: 1
Here is the translated code:
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
List<String> checkedItem = [];
List<Map<String, dynamic>> data = [
{'title': 'checkbox1', 'value': false},
{'title': 'checkbox2', 'value': false},
{'title': 'checkbox3', 'value': false}
];
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
// title: Text(widget.title),
),
body: Column(
children: [
Column(
children: checkedItem.map((e) => Text(e)).toList(),
),
ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
itemCount: data.length,
itemBuilder: (context, index) {
return CheckboxListTile(
title: Text(data[index]['title']),
value: data[index]['value'],
onChanged: (value) {
setState(() {
if (data[index]['value'] = value!) {
checkedItem.add(data[index]['title'].toString());
} else {
checkedItem.remove(data[index]['title']);
}
});
});
},
),
],
),
);
}
}
I've translated the code for you as requested.
英文:
Instead of using List<Widget>
you can use list of String to hold items and then you can easily remove items,
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
List<String> checkedItem = [];
List<Map<String, dynamic>> data = [
{'title': 'checkbox1', 'value': false},
{'title': 'checkbox2', 'value': false},
{'title': 'checkbox3', 'value': false}
];
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
// title: Text(widget.title),
),
body: Column(
children: [
Column(
children: checkedItem.map((e) => Text(e)).toList(),
),
ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
itemCount: data.length,
itemBuilder: (context, index) {
return CheckboxListTile(
title: Text(data[index]['title']),
value: data[index]['value'],
onChanged: (value) {
setState(() {
// bool check=data[index]['value'] = value!;
if (data[index]['value'] = value!) {
checkedItem.add(data[index]['title'].toString());
} else {
checkedItem.remove(data[index]['title']);
}
});
});
},
),
],
),
);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论