2

我正在尝试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 但同样的错误。

如何修复此自动生成运行器?

4

3 回答 3

10

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

dependency_overrides:
  analyzer: '0.39.14'

到 pubspec.yaml

于 2020-08-10T06:28:44.330 回答
2

2 个选项

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

或者

如前所述,添加

analyzer: '0.39.14'dependency_overrides

于 2020-08-10T08:48:21.747 回答
1

对我来说,修复是升级analyzer: '0.39.15' 我正在使用sdk: ">=2.10.0 <3.0.0"

于 2020-11-06T14:40:46.033 回答