我 运行 陷入 Switches in flutter 的问题

I ran into a problem with Switches in flutter

我在最高级别,运行遇到了问题。我有 4 个由构建器创建的开关:

//here are variables used later to pass data into builder
  bool _glutenFree = false;
  bool _vegetarian = false;
  bool _vegan = false;
  bool _lactoseFree = false;

Widget _buildSwitchListTile(
String title,
String description,
bool currentValue,
Function(bool) updateValue,


) {
    return SwitchListTile(
      title: Text(title),
      value: _glutenFree,
      subtitle: Text(description),
      onChanged: updateValue,
    );
  }

当我尝试点击第 2、3、4 个开关时,没有任何反应。 但是当我尝试点击第一个时,所有四个都会切换。

以下是 _buildSwitchListTile 的使用方式:

_buildSwitchListTile(
                'Gluten-free',
                'Only include gluten-free meals.',
                _glutenFree,
                (newValue) {
                  setState(
                    () {
                      _glutenFree = newValue;
                    },
                  );
                },
              ),
              _buildSwitchListTile(
                'Lactose-free',
                'Only include Lactose-free meals.',
                _lactoseFree,
                (newValue) {
                  setState(
                    () {
                      _lactoseFree = newValue;
                    },
                  );
                },
              ),
              _buildSwitchListTile(
                'Vegetarian',
                'Only include Vegetarian meals.',
                _vegetarian,
                (newValue) {
                  setState(
                    () {
                      _vegetarian = newValue;
                    },
                  );
                },
              ),
              _buildSwitchListTile(
                'Vegan',
                'Only include Vegan meals.',
                _vegan,
                (newValue) {
                  setState(
                    () {
                      _vegan = newValue;
                    },
                  );
                },
              ),

我知道Max的课程现在有点过时了,我通常自己分析问题,但在这里似乎一切都合乎逻辑。有没有人遇到过类似的问题?

将 _glutenFree 更改为当前值

    //here are variables used later to pass data into builder
    bool _glutenFree = false;
    bool _vegetarian = false;
    bool _vegan = false;
    bool _lactoseFree = false;

    Widget _buildSwitchListTile(
    String title,
    String description,
    bool currentValue,
    Function(bool) updateValue,


    ) {
     return SwitchListTile(
      title: Text(title),
      value: currentValue,    // change _glutenFree to currentValue
      subtitle: Text(description),
      onChanged: updateValue,
    );
  }