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

时间:2018-07-17 01:48:10

标签: flutter flutter-dependencies

我有以下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"
        }
    ]
}

我已经使用build_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反序列化的seralizer:

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的错误消息
  

常量创建的参数必须为常量表达式

     

无效的常量值

     

未定义名称“ BuiltList”

     

未定义的类“ 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();

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:0)

以防万一!导入

import 'package:built_collection/built_collection.dart';

在您的 serializers.dart 中 它将反映在 serializers.g.dart