【问题标题】:Bad state Unexpected diagnostics on json_serializable package with flutter错误状态 json_serializable 包上的意外诊断与颤动
【发布时间】:2020-11-29 17:32:59
【问题描述】:

我正在尝试在 Flutter 应用中为我的模型自动生成 json_serializable 代码。

这是我的模型示例:

import 'package:propro/src/models/product_model.dart';
import 'package:propro/src/models/user/address_model.dart';
import 'package:propro/src/models/user/membership_model.dart';
import 'package:propro/src/models/user/review_model.dart';
import 'package:propro/src/models/user/setting_model.dart';
import 'package:json_annotation/json_annotation.dart';

part 'user_model.g.dart';

@JsonSerializable(explicitToJson: true)
class User {
  final String uid;
  final String email;
  final String password;
  final String firstName;
  final String lastName;
  final String gender;
  final List<Address> addresses;
  final List<Review> reviews;
  final List<Product> wishlist;
  final Membership membership;
  final Setting setting;

  User({
    this.uid,
    this.email,
    this.password,
    this.firstName,
    this.lastName,
    this.gender,
    this.addresses,
    this.reviews,
    this.wishlist,
    this.membership,
    this.setting,
  });

  factory User.fromJson(Map<String, dynamic> json) => _UserFromJson(json);

  Map<String, dynamic> toJson() => _UserToJson(this);
}

这是我的 pubspec.yaml

dev_dependencies:
  flutter_test:
    sdk: flutter
  build_runner:
  json_serializable:

还有命令:

flutter packages pub run build_runner build

我有这个:

PS C:\tofiq\fp\propro> flutter packages pub run build_runner build
[INFO] Generating build script...
[INFO] Generating build script completed, took 755ms

[WARNING] Deleted previous snapshot due to missing asset graph.
[INFO] Creating build script snapshot......
[INFO] Creating build script snapshot... completed, took 22.3s

[INFO] Initializing inputs
[INFO] Building new asset graph...
[INFO] Building new asset graph completed, took 1.1s   

[INFO] Checking for unexpected pre-existing outputs....
[INFO] Checking for unexpected pre-existing outputs. completed, took 2ms

[INFO] Running build...
[INFO] Generating SDK summary...
[SEVERE] json_serializable:json_serializable on lib/main.dart:

Bad state: Unexpected diagnostics:
C:\tofiq\flutter\bin\cache\pkg\sky_engine\lib\ui\channel_buffers.dart:119:41 - This requires the 'non-nullable' language feature to be enabled.
C:\tofiq\flutter\bin\cache\pkg\sky_engine\lib\ui\channel_buffers.dart:152:17 - This requires the 'non-nullable' language feature to be enabled.
C:\tofiq\flutter\bin\cache\pkg\sky_engine\lib\ui\channel_buffers.dart:88:62 - This requires the 'non-nullable' language feature to be enabled. 
C:\tofiq\flutter\bin\cache\pkg\sky_engine\lib\ui\channel_buffers.dart:153:38 - This requires the 'non-nullable' language feature to be enabled.
C:\tofiq\flutter\bin\cache\pkg\sky_engine\lib\ui\channel_buffers.dart:186:51 - This requires the 'non-nullable' language feature to be enabled.
C:\tofiq\flutter\bin\cache\pkg\sky_engine\lib\ui\channel_buffers.dart:133:32 - This requires the 'non-nullable' language feature to be enabled.
C:\tofiq\flutter\bin\cache\pkg\sky_engine\lib\ui\channel_buffers.dart:154:25 - This requires the 'non-nullable' language feature to be enabled.
C:\tofiq\flutter\bin\cache\pkg\sky_engine\lib\ui\channel_buffers.dart:18:17 - This requires the 'non-nullable' language feature to be enabled. 
C:\tofiq\flutter\bin\cache\pkg\sky_engine\lib\ui\channel_buffers.dart:64:4 - This requires the 'non-nullable' language feature to be enabled.  
C:\tofiq\flutter\bin\cache\pkg\sky_engine\lib\ui\channel_buffers.dart:168:32 - This requires the 'non-nullable' language feature to be enabled.
C:\tofiq\flutter\bin\cache\pkg\sky_engine\lib\ui\channel_buffers.dart:47:14 - This requires the 'non-nullable' language feature to be enabled. 
C:\tofiq\flutter\bin\cache\pkg\sky_engine\lib\ui\channel_buffers.dart:159:38 - This requires the 'non-nullable' language feature to be enabled.
C:\tofiq\flutter\bin\cache\pkg\sky_engine\lib\ui\channel_buffers.dart:132:37 - This requires the 'non-nullable' language feature to be enabled.
C:\tofiq\flutter\bin\cache\pkg\sky_engine\lib\ui\channel_buffers.dart:118:48 - This requires the 'non-nullable' language feature to be enabled.
C:\tofiq\flutter\bin\cache\pkg\sky_engine\lib\ui\channel_buffers.dart:19:11 - This requires the 'non-nullable' language feature to be enabled. 
[SEVERE] json_serializable:json_serializable on lib/main.dart:
[INFO] 5.7s elapsed, 1/17 actions completed.
[INFO] 6.8s elapsed, 1/17 actions completed.
[INFO] 7.8s elapsed, 1/17 actions completed.
[INFO] 8.9s elapsed, 1/17 actions completed.
[INFO] 10.0s elapsed, 1/17 actions completed.
[INFO] 11.0s elapsed, 1/17 actions completed.
[INFO] 12.1s elapsed, 1/17 actions completed.

还有我的颤振版本信息:

PS C:\tofiq\fp\propro> flutter upgrade
Flutter is already up to date on channel beta
Flutter 1.20.0 • channel beta • https://github.com/flutter/flutter.git
Framework • revision 916c3ac648 (9 days ago) • 2020-08-01 09:01:12 -0700
Engine • revision d6ee1499c2
Tools • Dart 2.9.0 (build 2.9.0-21.10.beta)

我已经尝试过使用不同版本的包和 build_runner 但同样的错误。

如何修复这个自动生成的运行器?

【问题讨论】:

    标签: json flutter serializable build-runner


    【解决方案1】:

    我通过添加解决了这个问题

    dependency_overrides:
      analyzer: '0.39.14'
    

    到 pubspec.yaml

    【讨论】:

      【解决方案2】:

      2 个选项

      将频道更改为 dev(dart 2.10+ 可用)

      如前所述,添加

      analyzer: '0.39.14'dependency_overrides 部分

      【讨论】:

        【解决方案3】:

        对我来说,修复是升级analyzer: '0.39.15' 我正在使用sdk: "&gt;=2.10.0 &lt;3.0.0"

        【讨论】:

          猜你喜欢
          • 2020-12-02
          • 2021-01-06
          • 2016-08-01
          • 2019-12-02
          • 2019-01-29
          • 2022-01-04
          • 1970-01-01
          • 2019-05-10
          • 2023-03-10
          相关资源
          最近更新 更多