在Flutter列表中添加和移除数值。

huangapple go评论99阅读模式
英文:

adding and removing values in a flutter lists

问题

下面是您要翻译的代码部分:

  1. class cartMock extends StatefulWidget {
  2. const cartMock({Key? key}) : super(key: key);
  3. @override
  4. State<cartMock> createState() => _cartMockState();
  5. }
  6. class _cartMockState extends State<cartMock> {
  7. String? _selectedFruit;
  8. final List<String> _fruits = [];
  9. void _addItemToList(String item) {
  10. if (_fruits.contains(item)) {
  11. // if the item already exists in the list, remove it
  12. _fruits.remove(item);
  13. }
  14. // add the new item to the list
  15. _fruits.add(item);
  16. }
  17. @override
  18. Widget build(BuildContext context) {
  19. return Scaffold(
  20. body: Column(
  21. children: [
  22. Expanded(
  23. child: ListView.builder(
  24. itemCount: _fruits.length,
  25. itemBuilder: (context, index) {
  26. return ListTile(
  27. title: Text(_fruits[index]),
  28. );
  29. },
  30. ),
  31. ),
  32. RadioListTile(
  33. value: "apple",
  34. groupValue: _selectedFruit,
  35. onChanged: (value) {
  36. setState(() {
  37. if (value != _selectedFruit) {
  38. _selectedFruit = value as String?;
  39. _addItemToList("apple");
  40. }
  41. });
  42. },
  43. title: const Text("Apple"),
  44. ),
  45. RadioListTile(
  46. value: "orange",
  47. groupValue: _selectedFruit,
  48. onChanged: (value) {
  49. setState(() {
  50. if (value != _selectedFruit) {
  51. _selectedFruit = value as String?;
  52. _addItemToList("orange");
  53. }
  54. });
  55. },
  56. title: const Text("Orange"),
  57. ),
  58. ],
  59. ),
  60. );
  61. }
  62. }

希望这对您有所帮助。如果您有任何其他问题,请随时提出。

英文:

so below i have a list where for every time I tap a specific radio button value of the button gets added into an empty list , so what i'm trying to do i only want one value to only ever be contained in the list , meaning if i tap a radio button and the the value is placed in the list , the next radio button i tap must push out the former radios value and newly tapped radio button's value must be the only one in the list . my code above displays both values when i tap both radios , how can i fix this ?

  1. class cartMock extends StatefulWidget {
  2. const cartMock({Key? key}) : super(key: key);
  3. @override
  4. State&lt;cartMock&gt; createState() =&gt; _cartMockState();
  5. }
  6. class _cartMockState extends State&lt;cartMock&gt; {
  7. String? _selectedFruit;
  8. final List&lt;String&gt; _fruits = [];
  9. void _addItemToList(String item) {
  10. if (_fruits.contains(item)) {
  11. // if the item already exists in the list, remove it
  12. _fruits.remove(item);
  13. }
  14. // add the new item to the list
  15. _fruits.add(item);
  16. }
  17. @override
  18. Widget build(BuildContext context) {
  19. return Scaffold(
  20. body: Column(
  21. children: [
  22. Expanded(
  23. child: ListView.builder(
  24. itemCount: _fruits.length,
  25. itemBuilder: (context, index) {
  26. return ListTile(
  27. title: Text(_fruits[index]),
  28. );
  29. },
  30. ),
  31. ),
  32. RadioListTile(
  33. value: &quot;apple&quot;,
  34. groupValue: _selectedFruit,
  35. onChanged: (value) {
  36. setState(() {
  37. if (value != _selectedFruit) {
  38. _selectedFruit = value as String?;
  39. _addItemToList(&quot;apple&quot;);
  40. }
  41. });
  42. },
  43. title: const Text(&quot;Apple&quot;),
  44. ),
  45. RadioListTile(
  46. value: &quot;orange&quot;,
  47. groupValue: _selectedFruit,
  48. onChanged: (value) {
  49. setState(() {
  50. if (value != _selectedFruit) {
  51. _selectedFruit = value as String?;
  52. _addItemToList(&quot;orange&quot;);
  53. }
  54. });
  55. },
  56. title: const Text(&quot;Orange&quot;),
  57. ),
  58. ],
  59. ),
  60. );
  61. }
  62. }

答案1

得分: 0

只需像这样终止_addItemToList方法:

  1. void _addItemToList(String item) {
  2. if (_fruits.contains(item)) {
  3. // 如果项目已经存在于列表中,请将其移除
  4. _fruits.remove(item);
  5. return; // <----------------- 在这里添加 return
  6. }
  7. // 将新项目添加到列表中
  8. _fruits.add(item);
  9. }
英文:

You just need to terminate _addItemToList methode like this

  1. void _addItemToList(String item) {
  2. if (_fruits.contains(item)) {
  3. // if the item already exists in the list, remove it
  4. _fruits.remove(item);
  5. return; &lt;----------------- add return here
  6. }
  7. // add the new item to the list
  8. _fruits.add(item);
  9. }

huangapple
  • 本文由 发表于 2023年2月27日 15:57:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75577973.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定