Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have the following json object

{
    "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"
        }
    ]
}

I have created the following object model using 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;
}

And the following seralizer for json deserialization:

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();

The login object model worked fine and is working as expected but when I added the NotificationList to SerializersFor, the serializer.g.dart file started throwing errors. I am getting this error message for BuiltList in the serializers.g.dart file

Arguments of a constant creation must be constant expression

Invalid constant value

Undefined name 'BuiltList'

Undefined class 'ListBuilder'

On trying to compile I am getting this error which says the same thing

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:                     ^^^^^^^^^^^

Not sure where I am going wrong or what the issue could be. Using the following versions for built_value and built_collection

built_value: "^5.5.3"
built_collection: "^3.1.1"
build_runner: ^0.8.0
built_value_generator: ^5.5.0

This is the serializer.g.dart file generated:

// 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();

Any help will be greatly appreciated.

like image 999
ssiddh Avatar asked Dec 02 '22 10:12

ssiddh


2 Answers

It looks like serializers.g.dart is missing an import for built_collection, which is where you find those type definitions. Add an import statement for it to serializers.dart (rather than the generated part file) and see if that does the trick.

like image 156
RedBrogdon Avatar answered Dec 21 '22 10:12

RedBrogdon


In case that doesn't work! import

import 'package:built_collection/built_collection.dart';

In your serializers.dart It will reflect in serializers.g.dart

like image 45
SilenceCodder Avatar answered Dec 21 '22 09:12

SilenceCodder