Skip to content

Commit

Permalink
Merge pull request #3 from KosukeSaigusa/v0.0.2
Browse files Browse the repository at this point in the history
V0.0.2
  • Loading branch information
kosukesaigusa authored Jun 5, 2023
2 parents 8237563 + 808050e commit b327a5c
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 13 deletions.
47 changes: 46 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ flutterfire_json_converters package supplies some useful json_converters when yo

### Requirements

This package uses Dart 3 features, so you need to use compatible Dart (Flutter SDK).
This package uses Dart 3 features, so you need to use compatible Dart (Flutter) SDK.

```yaml
environment:
Expand Down Expand Up @@ -40,6 +40,51 @@ When dealing with Cloud Firestore's `Timestamp` field, you may encounter the fol
- During the creation or updating of Cloud Firestore documents, you may want to automatically set certain fields (e.g., `updatedAt`) to `FieldValue.serverTimestamp()`.
- For other fields (e.g., `createdAt`), you might sometimes prefer to specify a client-side DateTime and save it to a Cloud Firestore document, while at other times you might want to automatically set it to `FieldValue.serverTimestamp()`.

For such scenarios, you can define `DateTime`/`Timestamp` fields as `SealedTimestamp` type with `@sealedTimestampConverter`/`@alwaysUseServerTimestampSealedTimestampConverter` json_converter annotations like below:

```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);
}
```

When you want to get `DateTime` value of their fields on Dart client-side, you can call `SealedTimestamp.dateTime`.

```dart
final epoch = DateTime(1970);
final entity = Entity(name: 'foo', createdAt: ClientDateTime(epoch));
print(entity.name); // 'foo'
print(entity.createdAt.dateTime) // 1970-01-01 00:00:00.000
print(entity.updatedAt.dateTime) // null
final json = entity.toJson();
print(json['name']); // 'foo'
print(json['createdAt']); // Timestamp(seconds=0, nanoseconds=0)
print(json['updatedAt']); // FieldValue(Instance of 'MethodChannelFieldValue')
```

## Acknowledgements

This package is developed with reference to:
Expand Down
1 change: 1 addition & 0 deletions example/build.yaml
7 changes: 3 additions & 4 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,19 @@ class Entity {
this.updatedAt = const ServerTimestamp(),
});

/// Connect the generated [_$EntityFromJson] function to the `fromJson`
/// factory.
factory Entity.fromJson(Map<String, dynamic> json) => _$EntityFromJson(json);

/// The generated code assumes these values exist in 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;

/// Connect the generated [_$EntityToJson] function to the `toJson` method.
Map<String, dynamic> toJson() => _$EntityToJson(this);
}

Expand Down
4 changes: 3 additions & 1 deletion example/lib/main.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: flutterfire_json_converters
description: flutterfire_json_converters package supplies some useful json_converters for your FlutterFire apps.
version: 0.0.1
homepage: https://github.com/KosukeSaigusa/flutterfire_json_converters
homepage: https://github.com/KosukeSaigusa/flutterfire-json-converters

environment:
sdk: '>=3.0.0 <4.0.0'
Expand Down
2 changes: 0 additions & 2 deletions test/flutterfire_json_converters_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ void main() {
final entity = Entity(name: 'foo', createdAt: ClientDateTime(epoch));
final json = entity.toJson();

print(json['updatedAt']);

expect(json['name'], 'foo');
expect(json['createdAt'], Timestamp.fromDate(epoch));
expect(json['updatedAt'], isA<FieldValue>());
Expand Down
7 changes: 3 additions & 4 deletions test/helper/entity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,18 @@ class Entity {
this.updatedAt = const ServerTimestamp(),
});

/// Connect the generated [_$EntityFromJson] function to the `fromJson`
/// factory.
factory Entity.fromJson(Map<String, dynamic> json) => _$EntityFromJson(json);

/// The generated code assumes these values exist in 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;

/// Connect the generated [_$EntityToJson] function to the `toJson` method.
Map<String, dynamic> toJson() => _$EntityToJson(this);
}

0 comments on commit b327a5c

Please sign in to comment.