gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 06:47:43 27 4
gpt4 key购买 nike

我有以下 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 的导入,您可以在其中找到这些类型定义。将它的 import 语句添加到 serializers.dart(而不是生成的零件文件),看看是否能解决问题。

关于flutter - Dart built_Value,序列化程序中的 BuiltList 错误 - 常量创建的参数必须是常量表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51372133/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com