Json_serializable.dart: How to serialize the mongodb '_id' ?

Created on 19 Jul 2018  路  1Comment  路  Source: google/json_serializable.dart

Hi,
I have a problem to serialize mongodb document, because of the _id field.
For example,
account collection is

{ _id: 1, name: 'test'}

account.dart file is like below:

import 'package:json_annotation/json_annotation.dart';
part 'account.g.dart';

class Account extends Object with _$AccountSerializerMixin {
  // final String id;
  @JsonKey(name: '_id')
  String _id;
  String name;


  Account(
      this._id,
      this.name,
);
  factory Account.fromJson(Map<String, dynamic> json) =>
      _$AccountFromJson(json);
}

it will fail to run flutter packages pub run build_runner watch. Any idea about the problem?

Most helpful comment

Oh, I resolved it:

@JsonKey(name: '_id')
String id;

 Account(
      this.id,
      this.name,
)

>All comments

Oh, I resolved it:

@JsonKey(name: '_id')
String id;

 Account(
      this.id,
      this.name,
)
Was this page helpful?
0 / 5 - 0 ratings

Related issues

fengqiangboy picture fengqiangboy  路  5Comments

emiliodallatorre picture emiliodallatorre  路  3Comments

HosseinArabbeigi picture HosseinArabbeigi  路  3Comments

broofus picture broofus  路  6Comments

4BooM04 picture 4BooM04  路  3Comments