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

为何WidgetStatePropertyAll无法识别?Flutter 3.22.0报错求助

关于Flutter 3.22.0中WidgetStatePropertyAll报错的问题

我已将Flutter升级至最新稳定版3.22.0,但WidgetStatePropertyAll出现报错,该代码在其他系统曾正常运行。执行flutter clean和flutter pub get后错误暂时消失,但运行应用时错误再次出现并显示在问题标签页中,相关代码如下:

class ForgotPasswordPage extends StatefulWidget {
  const ForgotPasswordPage({super.key});

  @override
  State<ForgotPasswordPage> createState() => _ForgotPasswordPageState();
}

class _ForgotPasswordPageState extends State<ForgotPasswordPage> {
  void _passwordReset(BuildContext context) {
    context.read<AuthCubit>().resetPassword(_emailController.text.trim());
  }

  final TextEditingController _emailController = TextEditingController();

  @override
  void dispose() {
    _emailController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Theme.of(context).colorScheme.surface,
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Center(
            child: Text(
              'Enter your email address and we will send you a password reset link',
              textAlign: TextAlign.center,
              style: TextStyle(
                  color: Theme.of(context).colorScheme.onPrimaryContainer,
                  fontSize: 20),
            ),
          ),
          const SizedBox(height: 25),
          SizedBox(
            height: 50,
            width: 320,
            child: TextField(
              controller: _emailController,
              style: TextStyle(
                  color: Theme.of(context).colorScheme.onPrimaryContainer),
              key: const ValueKey('email'),
              keyboardType: TextInputType.emailAddress,
              decoration: InputDecoration(
                labelText: 'Email Address',
                labelStyle: TextStyle(
                    color: Theme.of(context).colorScheme.onPrimaryContainer),
                enabledBorder: OutlineInputBorder(
                  borderSide:
                      BorderSide(color: Theme.of(context).colorScheme.outline),
                  borderRadius: BorderRadius.circular(10),
                ),
              ),
            ),
          ),
          const SizedBox(
            height: 25,
          ),
          SizedBox(
            width: 320,
            height: 50,
            child: ElevatedButton(
              style: ButtonStyle(
                  backgroundColor: WidgetStatePropertyAll(
                      Theme.of(context).colorScheme.primary)),
              onPressed: () => _passwordReset(context),
              child: Text(
                'Reset Password',
                style: TextStyle(
                    fontSize: 17,
                    color: Theme.of(context).colorScheme.onPrimary),
              ),
            ),
          ),
          const SizedBox(height: 25),
          SizedBox(
            width: 320,
            height: 50,
            child: ElevatedButton(
              style: ButtonStyle(
                  backgroundColor: WidgetStatePropertyAll(
                      Theme.of(context).colorScheme.primary)),
              onPressed: () => Navigator.pop(context),
              child: Text(
                'Go back to login screen',
                style: TextStyle(
                    fontSize: 17,
                    color: Theme.of(context).colorScheme.onPrimary),
              ),
            ),
          ),
        ],
      ),
    );
  }
}

问题原因及解决方案

这个问题确实和Flutter版本直接相关:

  • Flutter 3.10版本开始,WidgetStatePropertyAll被标记为废弃,官方推荐使用WidgetStateProperty.all()作为替代;
  • 到Flutter 3.22.0稳定版,该废弃API的校验逻辑被严格执行,导致原本能运行的代码触发报错。

修复步骤

  1. 将代码中所有的WidgetStatePropertyAll替换为WidgetStateProperty.all(),修改后的按钮样式代码示例:
style: ButtonStyle(
    backgroundColor: WidgetStateProperty.all(
        Theme.of(context).colorScheme.primary)),
  1. 执行flutter clean和flutter pub get清除缓存并重新拉取依赖;
  2. 运行应用,报错即可解决。

额外建议

升级Flutter版本后,建议执行flutter analyze命令扫描项目,排查其他可能存在的废弃API使用情况,提前规避类似问题。

内容的提问来源于stack exchange,提问作者M99Coder

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 01:04:56