执行flutter pub run build_runner build生成JSON序列化器遇super-parameters错误
解决Flutter build_runner生成JSON序列化代码时的super-parameters错误
以下是针对问题的有效解决步骤:
修正analysis_options.yml配置
你之前添加的是lint规则,无法启用目标语言特性。需要在项目根目录的analysis_options.yml中添加语言特性启用配置:analyzer: language: enableSuperParameters: true完善序列化类代码
你的HttpErrorResponse类缺少必要的序列化方法和构造函数,补全后才能让build_runner正确生成代码:import 'package:json_annotation/json_annotation.dart'; part 'HttpErrorResponse.g.dart'; @JsonSerializable() class HttpErrorResponse { late final String type; late final String title; late final int status; late final String detail; late final String instance; HttpErrorResponse(); factory HttpErrorResponse.fromJson(Map<String, dynamic> json) => _$HttpErrorResponseFromJson(json); Map<String, dynamic> toJson() => _$HttpErrorResponseToJson(this); }清理缓存并重新生成
执行以下命令彻底清理缓存、更新依赖并重新生成代码:flutter pub cache clean flutter pub get flutter pub run build_runner build --delete-conflicting-outputs确保依赖版本兼容
更新pubspec.yml中的依赖版本,适配Flutter 3.7.0(Dart 2.19.0):dependencies: json_annotation: ^4.8.1 dev_dependencies: json_serializable: ^6.7.0执行
flutter pub upgrade完成版本更新。
内容的提问来源于stack exchange,提问作者Camilo Vasquez
相关产品推荐
相关产品推荐

