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

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

adding and removing values in a flutter lists

问题

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

class cartMock extends StatefulWidget {
  const cartMock({Key? key}) : super(key: key);

  @override
  State<cartMock> createState() => _cartMockState();
}

class _cartMockState extends State<cartMock> {
  String? _selectedFruit;
  final List<String> _fruits = [];

  void _addItemToList(String item) {
    if (_fruits.contains(item)) {
      // if the item already exists in the list, remove it
      _fruits.remove(item);
    }
    // add the new item to the list
    _fruits.add(item);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          Expanded(
            child: ListView.builder(
              itemCount: _fruits.length,
              itemBuilder: (context, index) {
                return ListTile(
                  title: Text(_fruits[index]),
                );
              },
            ),
          ),
          RadioListTile(
            value: "apple",
            groupValue: _selectedFruit,
            onChanged: (value) {
              setState(() {
                if (value != _selectedFruit) {
                  _selectedFruit = value as String?;
                  _addItemToList("apple");
                }
              });
            },
            title: const Text("Apple"),
          ),
          RadioListTile(
            value: "orange",
            groupValue: _selectedFruit,
            onChanged: (value) {
              setState(() {
                if (value != _selectedFruit) {
                  _selectedFruit = value as String?;
                  _addItemToList("orange");
                }
              });
            },
            title: const Text("Orange"),
          ),
        ],
      ),
    );
  }
}

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

英文:

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 ?

class cartMock extends StatefulWidget {
  const cartMock({Key? key}) : super(key: key);

  @override
  State&lt;cartMock&gt; createState() =&gt; _cartMockState();
}

class _cartMockState extends State&lt;cartMock&gt; {
  String? _selectedFruit;
  final List&lt;String&gt; _fruits = [];

  void _addItemToList(String item) {
    if (_fruits.contains(item)) {
      // if the item already exists in the list, remove it
      _fruits.remove(item);
    }
    // add the new item to the list
    _fruits.add(item);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          Expanded(
            child: ListView.builder(
              itemCount: _fruits.length,
              itemBuilder: (context, index) {
                return ListTile(
                  title: Text(_fruits[index]),
                );
              },
            ),
          ),
          RadioListTile(
            value: &quot;apple&quot;,
            groupValue: _selectedFruit,
            onChanged: (value) {
              setState(() {
                if (value != _selectedFruit) {
                  _selectedFruit = value as String?;
                  _addItemToList(&quot;apple&quot;);
                }
              });
            },
            title: const Text(&quot;Apple&quot;),
          ),
          RadioListTile(
            value: &quot;orange&quot;,
            groupValue: _selectedFruit,
            onChanged: (value) {
              setState(() {
                if (value != _selectedFruit) {
                  _selectedFruit = value as String?;
                  _addItemToList(&quot;orange&quot;);
                }
              });
            },
            title: const Text(&quot;Orange&quot;),
          ),
        ],
      ),
    );
  }
}

答案1

得分: 0

只需像这样终止_addItemToList方法:

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

You just need to terminate _addItemToList methode like this

void _addItemToList(String item) {
    if (_fruits.contains(item)) {
      // if the item already exists in the list, remove it
      _fruits.remove(item);
       return;  &lt;----------------- add return here
    }
    // add the new item to the list
    _fruits.add(item);
  }

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:

确定