json_serializable问题

来源:3-10 三种JSON解析技巧带你解放生产力

weixin_慕UI2387872

2024-02-29

老师,在执行命令:flutter packages pub run build_runner build
build生成实体类时候,出现了问题,求解
依赖版本:

dependencies:
  dio: ^5.4.1
  json_annotation: ^4.8.1

dev_dependencies:
  json_serializable: ^6.7.1
  build_runner: ^2.4.8

result.dart

import 'package:json_annotation/json_annotation.dart';

part 'result.g.dart';

@JsonSerializable()
class Result {
  // 定义字段
  int? code;
  String? method;
  String? requestParams;

  Result(this.code, this.method, this.requestParams);
  // 固定格式,不同的类使用不同的mixin即可
  factory Result.fromJson(Map<String, dynamic> json) => _$ResultFromJson(json);
  Map<String, dynamic> toJson() => _$ResultToJson(this);
}

报错:

Deprecated. Use `dart run` instead.
Building package executable... (2.0s)
Failed to build build_runner:build_runner:
../../.pub-cache/hosted/pub.flutter-io.cn/dart_style-2.3.5/lib/src/source_visitor.dart:595:19: Error: The getter 'macroKeyword' isn't defined for the class 'ClassDeclaration'.
 - 'ClassDeclaration' is from 'package:analyzer/src/dart/ast/ast.dart' ('../../.pub-cache/hosted/pub.flutter-io.cn/analyzer-6.2.0/lib/src/dart/ast/ast.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'macroKeyword'.
    modifier(node.macroKeyword);
                  ^^^^^^^^^^^^
../../.pub-cache/hosted/pub.flutter-io.cn/dart_style-2.3.5/lib/src/front_end/ast_node_visitor.dart:251:16: Error: The getter 'macroKeyword' isn't defined for the class 'ClassDeclaration'.
 - 'ClassDeclaration' is from 'package:analyzer/src/dart/ast/ast.dart' ('../../.pub-cache/hosted/pub.flutter-io.cn/analyzer-6.2.0/lib/src/dart/ast/ast.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'macroKeyword'.
          node.macroKeyword,
               ^^^^^^^^^^^^

写回答

1回答

CrazyCodeBoy

2024-02-29

你将flutter和json_serializable以及build_runner都升级到最新版看看还有没有这个问题
0
1
weixin_慕UI2387872
谢谢老师,全部更新到最新+clean缓存后解决了!
2024-03-01
共1条回复

Flutter高级进阶实战-仿哔哩哔哩-掌握Flutter高阶技能

一次性掌握Flutter高阶技能+商业级复杂项目架构设计与开发方案

1723 学习 · 870 问题

查看课程