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

使用Flutter Bloc与Freezed状态管理时遇getter未实现错误求助

Flutter Bloc + Freezed 状态管理错误:缺失_$CounterState.count getter实现

错误详情

Missing concrete implementation of 'getter _$CounterState.count'.
Try implementing the missing method, or make the class abstract

错误截图

解决步骤

  • 优先运行Freezed代码生成命令,这是引发该错误的最常见原因:
    flutter pub run build_runner build
    # 或使用watch模式实时监听文件变化并生成代码
    flutter pub run build_runner watch
    
  • 检查CounterState类是否符合Freezed的注解规范:
    import 'package:freezed_annotation/freezed_annotation.dart';
    
    part 'counter_state.freezed.dart';
    
    @freezed
    class CounterState with _$CounterState {
      const factory CounterState({required int count}) = _CounterState;
    }
    
    确保类上添加了@freezed注解,并且通过with _$CounterState继承生成类,同时工厂构造函数中正确声明了count字段。
  • 确认pubspec.yaml的依赖配置完整:
    dependencies:
      freezed_annotation: ^2.4.1
      flutter_bloc: ^8.1.5
    
    dev_dependencies:
      build_runner: ^2.4.8
      freezed: ^2.4.7
    
    执行flutter pub get安装依赖后,重新运行代码生成命令。
  • 若已存在生成文件但报错未消失,可删除项目根目录下的.dart_tool和build文件夹,再重新执行代码生成命令。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 01:26:01