Dart built_Value,序列化程序中的 BuiltList 错误 - 常量创建的参数必须是常量表达式

Dart built_Value, BuiltList error in serializer - Arguments of a constant creation must be constant expression

我有以下json对象

{
    "notifications": [
        {
            "correspondenceId": "81",
            "type": "notification",
            "title": "Find Your Future at Indiana University",
            "snippet": "",
            "readFlag": "NO",
            "date": "Delivered on: Jul 09, 2018 at 12:00 AM",
            "readDate": "Read on: Apr 03, 2018 at 12:00 AM",
            "icon": "message",
            "color": "neutral"
        },
        {
            "correspondenceId": "80",
            "type": "notification",
            "title": "My IU Experience",
            "snippet": "",
            "readFlag": "NO",
            "date": "Delivered on: Jul 09, 2018 at 12:00 AM",
            "readDate": "Read on: Apr 03, 2018 at 12:00 AM",
            "icon": "message",
            "color": "red"
        },
        {
            "correspondenceId": "82",
            "type": "notification",
            "title": "Test RSVP",
            "snippet": "",
            "readFlag": "NO",
            "date": "Delivered on: Jul 09, 2018 at 12:00 AM",
            "readDate": "Read on: Apr 10, 2018 at 04:31 PM",
            "icon": "message",
            "color": "neutral"
        }
    ]
}

我使用 built_value

创建了以下对象模型
import 'package:built_collection/built_collection.dart';
import 'package:built_value/built_value.dart';
import 'package:built_value/serializer.dart';

part 'notification.g.dart';

abstract class NotificationList
    implements Built<NotificationList, NotificationListBuilder> {
  BuiltList<NotificationElement> get notifications;

  NotificationList._();
  static Serializer<NotificationList> get serializer =>
      _$notificationListSerializer;
  factory NotificationList([updates(NotificationListBuilder b)]) =
      _$NotificationList;
}

abstract class NotificationElement
    implements Built<NotificationElement, NotificationElementBuilder> {
  String get correspondenceId;

  String get type;

  String get title;

  @nullable
  String get snippet;

  String get readFlag;

  bool get derivedReadFlag {
    return readFlag.contains("YES");
  }

  String get date;

  @nullable
  String get readDate;

  String get icon;

  String get color;

  NotificationElement._();
  static Serializer<NotificationElement> get serializer =>
      _$notificationElementSerializer;
  factory NotificationElement([updates(NotificationElementBuilder b)]) =
      _$NotificationElement;
}

以及以下用于 json 反序列化的序列化程序:

import 'package:built_value/serializer.dart';
import 'package:built_value/standard_json_plugin.dart';
import 'package:sunapsis/datasource/dataobjects/login.dart';
import 'package:sunapsis/datasource/dataobjects/notification.dart';

part 'serializers.g.dart';

@SerializersFor([
  Login,
  NotificationList,
])
final Serializers serializers =
    (_$serializers.toBuilder()..addPlugin(StandardJsonPlugin())).build();

登录对象模型工作正常并且按预期工作,但是当我将 NotificationList 添加到 SerializersFor 时,serializer.g.dart 文件开始抛出错误。我在 serializers.g.dart 文件

中收到有关 BuiltList 的错误消息

Arguments of a constant creation must be constant expression

Invalid constant value

Undefined name 'BuiltList'

Undefined class 'ListBuilder'

在尝试编译时我收到了这个错误,它说的是同样的事情

compiler message: lib/datasource/dataobjects/serializers.g.dart:24:15: Error: Getter not found: 'BuiltList'.
compiler message:               BuiltList, const [const FullType(NotificationElement)]),
compiler message:               ^
compiler message: lib/datasource/dataobjects/serializers.g.dart:25:21: Error: Method not found: 'ListBuilder'.
compiler message:           () => new ListBuilder<NotificationElement>()))
compiler message:                     ^^^^^^^^^^^

不确定我哪里出错了或者可能是什么问题。 对 built_value 和 built_collection

使用以下版本
built_value: "^5.5.3"
built_collection: "^3.1.1"
build_runner: ^0.8.0
built_value_generator: ^5.5.0

这是生成的serializer.g.dart文件:

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'serializers.dart';

// **************************************************************************
// BuiltValueGenerator
// **************************************************************************

// ignore_for_file: always_put_control_body_on_new_line
// ignore_for_file: annotate_overrides
// ignore_for_file: avoid_annotating_with_dynamic
// ignore_for_file: avoid_returning_this
// ignore_for_file: omit_local_variable_types
// ignore_for_file: prefer_expression_function_bodies
// ignore_for_file: sort_constructors_first

Serializers _$serializers = (new Serializers().toBuilder()
      ..add(AuthProcess.serializer)
      ..add(Login.serializer)
      ..add(NotificationElement.serializer)
      ..add(NotificationList.serializer)
      ..addBuilderFactory(
          const FullType(
              BuiltList, const [const FullType(NotificationElement)]),
          () => new ListBuilder<NotificationElement>()))
    .build();

任何帮助将不胜感激。

看起来 serializers.g.dart 缺少 built_collection 的导入,您可以在其中找到这些类型定义。将它的导入语句添加到 serializers.dart(而不是生成的零件文件),看看是否能解决问题。

万一不行呢!进口

import 'package:built_collection/built_collection.dart';

在你的serializers.dart 会体现在serializers.g.dart