我有以下 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/
在我的gem中,我需要yaml并且在我的本地计算机上运行良好。但是在将我的gem推送到rubygems.org之后,当我尝试使用我的gem时,我收到一条错误消息=>"uninitializedconstantPsych::Syck(NameError)"谁能帮我解决这个问题?附言RubyVersion=>ruby1.9.2,GemVersion=>1.6.2,Bundlerversion=>1.0.15 最佳答案 经过几个小时的研究,我发现=>“YAML使用未维护的Syck库,而Psych使用现代的LibYAML”因此,为了解决
我正在使用active_admin,我在Rails3应用程序的应用程序中有一个目录管理,其中包含模型和页面的声明。时不时地我也有一个类,当那个类有一个常量时,就像这样:classFooBAR="bar"end然后,我在每个必须在我的Rails应用程序中重新加载一些代码的请求中收到此警告:/Users/pupeno/helloworld/app/admin/billing.rb:12:warning:alreadyinitializedconstantBAR知道发生了什么以及如何避免这些警告吗? 最佳答案 在纯Ruby中:classA
我收到这个错误:RuntimeError(自动加载常量Apps时检测到循环依赖当我使用多线程时。下面是我的代码。为什么会这样?我尝试多线程的原因是因为我正在编写一个HTML抓取应用程序。对Nokogiri::HTML(open())的调用是一个同步阻塞调用,需要1秒才能返回,我有100,000多个页面要访问,所以我试图运行多个线程来解决这个问题。有更好的方法吗?classToolsController0)app.website=array.join(',')putsapp.websiteelseapp.website="NONE"endapp.saveapps=Apps.order("
我有用于控制用户任务的Rails5API项目,我有以下错误,但并非总是针对相同的Controller和路由。ActionController::RoutingError:uninitializedconstantApi::V1::ApiController我向您描述了一些我的项目,以更详细地解释错误。应用结构路线scopemodule:'api'donamespace:v1do#=>Loginroutesscopemodule:'login'domatch'login',to:'sessions#login',as:'login',via::postend#=>Teamroutessc
我想获取模块中定义的所有常量的值:moduleLettersA='apple'.freezeB='boy'.freezeendconstants给了我常量的名字:Letters.constants(false)#=>[:A,:B]如何获取它们的值的数组,即["apple","boy"]? 最佳答案 为了做到这一点,请使用mapLetters.constants(false).map&Letters.method(:const_get)这将返回["a","b"]第二种方式:Letters.constants(false).map{|c
我早就知道Ruby中的“常量”(即大写的变量名)不是真正常量。与其他编程语言一样,对对象的引用是唯一存储在变量/常量中的东西。(侧边栏:Ruby确实具有“卡住”引用对象不被修改的功能,据我所知,许多其他语言都没有提供这种功能。)所以这是我的问题:当您将一个值重新分配给常量时,您会收到如下警告:>>FOO='bar'=>"bar">>FOO='baz'(irb):2:warning:alreadyinitializedconstantFOO=>"baz"有没有办法强制Ruby抛出异常而不是打印警告?很难弄清楚为什么有时会发生重新分配。 最佳答案
给定一个复杂的对象层次结构,幸运的是它不包含循环引用,我如何实现支持各种格式的序列化?我不是来讨论实际实现的。相反,我正在寻找可能会派上用场的设计模式提示。更准确地说:我正在使用Ruby,我想解析XML和JSON数据以构建复杂的对象层次结构。此外,应该可以将该层次结构序列化为JSON、XML和可能的HTML。我可以为此使用Builder模式吗?在任何提到的情况下,我都有某种结构化数据-无论是在内存中还是文本中-我想用它来构建其他东西。我认为将序列化逻辑与实际业务逻辑分开会很好,这样我以后就可以轻松支持多种XML格式。 最佳答案 我最
在我的系统中,我已经定义了STI。Dog继承自Animal,在animals表中有一个type列,其值为"Dog"。现在我想让SpecialDog继承自dog,只是为了在某些特殊情况下稍微修改一下行为。数据还是一样。我需要通过SpecialDog运行的所有查询,以返回数据库中类型为Dog的值。我的问题是因为我有一个type列,rails将WHERE"animals"."type"IN('SpecialDog')附加到我的查询中,所以我不能获取原始的Dog条目。所以我想要的是以某种方式覆盖rails在通过SpecialDog访问数据库时使用的值,使其表现得像Dog。有没有办法覆盖用于类型
了解Rails缓存如何工作的人可以真正帮助我。这是嵌套在Rails::Initializer.runblock中的代码:config.after_initializedoSomeClass.const_set'SOME_CONST','SOME_VAL'end现在,如果我运行script/server并发出请求,一切都很好。然而,在我的Rails应用程序的第二个请求中,一切都因单元化常量错误而变得糟糕。在生产模式下,我可以成功发出第二个请求,这意味着常量仍然存在。我已通过将以上内容更改为以下内容来解决问题:config.after_initializedorequire'some_cl
我想从then子句中访问case语句表达式,即food="cheese"casefoodwhen"dip"then"carrotsticks"when"cheese"then"#{expr}crackers"else"mayo"end在这种情况下,expr是食物的当前值(value)。在这种情况下,我知道,我可以简单地访问变量food,但是在某些情况下,该值可能无法再访问(array.shift等)。除了将expr移出到局部变量然后访问它之外,是否有直接访问caseexpr值的方法?罗亚附注我知道这个具体示例很简单,只是一个示例场景。 最佳答案