You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter中FilterScreen构建时触发NoSuchMethodError错误求助

Flutter NoSuchMethodError 错误修复

错误原因

你遇到的NoSuchMethodError(闭包调用参数不匹配),核心问题出在buildSwitchListTile方法里的onChanged参数传递:你写了updateValue(),这会立即执行这个闭包,但该闭包需要接收一个bool类型参数,无参调用直接触发了参数不匹配的错误。而且onChanged需要的是函数引用,不是函数执行后的返回值。

修复步骤

  1. 修正函数传递方式:把onChanged: updateValue()改成onChanged: updateValue,直接传递函数本身,让SwitchListTile在状态变化时自动传入参数调用它。
  2. 优化类型声明(可选但推荐):把buildSwitchListTile的Function updateValue参数,改成更明确的void Function(bool) updateValue,编译阶段就能检测类型错误,避免类似问题复发。

修改后的完整代码

import '../widgets/main-drawer.dart';
import 'package:flutter/material.dart';

class FilterScreen extends StatefulWidget {
  static const routeName = '/filters';

  @override
  State<FilterScreen> createState() => _FilterScreenState();
}

class _FilterScreenState extends State<FilterScreen> {
  bool _gluttenFree = false;
  bool _lactosFree = false;
  bool _vegan = false;
  bool _vegetarian = false;

  // 优化参数类型声明,增强类型安全
  Widget buildSwitchListTile(String title, String description, bool currentValue,
      void Function(bool) updateValue) {
    return SwitchListTile(
      title: Text(title),
      value: currentValue,
      // 去掉括号,直接传递函数引用
      onChanged: updateValue,
      subtitle: Text(description),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('筛选页面'),
      ),
      body: Column(
        children: [
          Container(
            padding: EdgeInsets.all(20),
            child: Text(
              "调整你的餐食选择",
              style: Theme.of(context).textTheme.titleMedium,
            ),
          ),
          Expanded(
            child: ListView(
              children: [
                buildSwitchListTile(
                  '无麸质',
                  '仅包含无麸质餐食',
                  _gluttenFree,
                  (newValue) {
                    setState(() {
                      _gluttenFree = newValue;
                    });
                  },
                ),
                buildSwitchListTile(
                  '无乳糖',
                  '仅包含无乳糖餐食',
                  _lactosFree,
                  (newValue) {
                    setState(() {
                      _lactosFree = newValue;
                    });
                  },
                ),
                buildSwitchListTile(
                  '纯素食',
                  '仅包含纯素餐食',
                  _vegan,
                  (newValue) {
                    setState(() {
                      _vegan = newValue;
                    });
                  },
                ),
                buildSwitchListTile(
                  '素食',
                  '仅包含素食餐食',
                  _vegetarian,
                  (newValue) {
                    setState(() {
                      _vegetarian = newValue;
                    });
                  },
                ),
              ],
            ),
          ),
        ],
      ),
      drawer: MainDrawer(),
    );
  }
}

内容的提问来源于stack exchange,提问作者Louay Elaroui pro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 09:36:28