我正在尝试在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:tofiqfppropro> 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:tofiqflutterbincachepkgsky_enginelibuichannel_buffers.dart:119:41 - This requires the 'non-nullable' language feature to be enabled.
C:tofiqflutterbincachepkgsky_enginelibuichannel_buffers.dart:152:17 - This requires the 'non-nullable' language feature to be enabled.
C:tofiqflutterbincachepkgsky_enginelibuichannel_buffers.dart:88:62 - This requires the 'non-nullable' language feature to be enabled.
C:tofiqflutterbincachepkgsky_enginelibuichannel_buffers.dart:153:38 - This requires the 'non-nullable' language feature to be enabled.
C:tofiqflutterbincachepkgsky_enginelibuichannel_buffers.dart:186:51 - This requires the 'non-nullable' language feature to be enabled.
C:tofiqflutterbincachepkgsky_enginelibuichannel_buffers.dart:133:32 - This requires the 'non-nullable' language feature to be enabled.
C:tofiqflutterbincachepkgsky_enginelibuichannel_buffers.dart:154:25 - This requires the 'non-nullable' language feature to be enabled.
C:tofiqflutterbincachepkgsky_enginelibuichannel_buffers.dart:18:17 - This requires the 'non-nullable' language feature to be enabled.
C:tofiqflutterbincachepkgsky_enginelibuichannel_buffers.dart:64:4 - This requires the 'non-nullable' language feature to be enabled.
C:tofiqflutterbincachepkgsky_enginelibuichannel_buffers.dart:168:32 - This requires the 'non-nullable' language feature to be enabled.
C:tofiqflutterbincachepkgsky_enginelibuichannel_buffers.dart:47:14 - This requires the 'non-nullable' language feature to be enabled.
C:tofiqflutterbincachepkgsky_enginelibuichannel_buffers.dart:159:38 - This requires the 'non-nullable' language feature to be enabled.
C:tofiqflutterbincachepkgsky_enginelibuichannel_buffers.dart:132:37 - This requires the 'non-nullable' language feature to be enabled.
C:tofiqflutterbincachepkgsky_enginelibuichannel_buffers.dart:118:48 - This requires the 'non-nullable' language feature to be enabled.
C:tofiqflutterbincachepkgsky_enginelibuichannel_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:tofiqfppropro> 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)
我已经尝试过不同版本的package和build_runner,但都出现了相同的错误。
如何修复这个自动生成的runner?
我通过添加解决了这个问题
dependency_overrides:
analyzer: '0.39.14'
到pubspec.yaml
2个选项
将频道更改为dev(其中dart 2.10+可用(
或
如前所述,添加
analyzer: '0.39.14'
至dependency_overrides
段
对我来说,修复程序是升级analyzer: '0.39.15'
我正在使用sdk: ">=2.10.0 <3.0.0"