flutterfire_json_converters 0.0.8 copy "flutterfire_json_converters: ^0.0.8" to clipboard
flutterfire_json_converters: ^0.0.8 copied to clipboard

flutterfire_json_converters package supplies some useful json_converters for your FlutterFire apps.

example/lib/main.dart

import 'package:flutterfire_json_converters/flutterfire_json_converters.dart';
import 'package:json_annotation/json_annotation.dart';

part 'main.g.dart';

@JsonSerializable()
class Entity {
  Entity({
    required this.name,
    this.createdAt = const ServerTimestamp(),
    this.updatedAt = const ServerTimestamp(),
  });

  factory Entity.fromJson(Map<String, dynamic> json) => _$EntityFromJson(json);

  final String name;

  // Handle both Dart's `DateTime` and Cloud Firestore's `Timestamp` types.
  @sealedTimestampConverter
  final SealedTimestamp createdAt;

  // Handle both Dart's `DateTime` and Cloud Firestore's `Timestamp` types, and
  // always use `FieldValue.serverTimestamp()` when creating/updating a document.
  @alwaysUseServerTimestampSealedTimestampConverter
  final SealedTimestamp updatedAt;

  Map<String, dynamic> toJson() => _$EntityToJson(this);
}

void main() {
  final epoch = DateTime(1970);
  final entity = Entity(name: 'foo', createdAt: ClientDateTime(epoch));
  final json = entity.toJson();

  print(json['name']); // 'foo'
  print(json['createdAt']); // Timestamp(seconds=0, nanoseconds=0)
  print(json['updatedAt']); // FieldValue(Instance of 'MethodChannelFieldValue')
}
12
likes
140
pub points
45%
popularity

Publisher

verified publisherkosukesaigusa.com

flutterfire_json_converters package supplies some useful json_converters for your FlutterFire apps.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

cloud_firestore, flutter, json_annotation

More

Packages that depend on flutterfire_json_converters