Git Product home page Git Product logo

Comments (13)

valle-xyz avatar valle-xyz commented on June 17, 2024 3

Yes I can see them in the freezed file, but they somehow don't get propagated down to the json_annotaded file.

from freezed.

GabrielAraujo avatar GabrielAraujo commented on June 17, 2024 1

This is also happening with me on versions:
freezed: ^0.14.5
json_serializable: ^5.0.2

And it is the same as @edgeboy47 stated, works on required but not on nullable attributes

Any news on this?

from freezed.

rrousselGit avatar rrousselGit commented on June 17, 2024

That's a good idea.

from freezed.

rrousselGit avatar rrousselGit commented on June 17, 2024

Closing in favor of #17

from freezed.

valle-xyz avatar valle-xyz commented on June 17, 2024

I am not sure how to use this functionality. I tried it like this:

@freezed
class NewsPost with _$NewsPost {
  factory NewsPost({
    @TimestampConverter() @JsonKey(name: "date") DateTime? date,
  }) = _NewsPost;

  factory NewsPost.fromJson(Map<String, dynamic> json) =>
      _$NewsPostFromJson(json);
}

Without success. Would you be so kind as to provide a hint on how to use it, @rrousselGit? Maybe this would even be a good addition to the documentation.

Dublicate of a stackoverflow question.

from freezed.

elianortega avatar elianortega commented on June 17, 2024

Hey @Valentin-Seehausen, what is exactly the problem?

I tried this code snippet:

import 'package:freezed_annotation/freezed_annotation.dart';
part 'post.freezed.dart';
part 'post.g.dart';

@freezed
class NewsPost with _$NewsPost {
  factory NewsPost({
    @JsonKey(name: "date") DateTime? date,
  }) = _NewsPost;

  factory NewsPost.fromJson(Map<String, dynamic> json) =>
      _$NewsPostFromJson(json);
}

and it generated the code successfully.

I don't understand the @TimestampConverter() annotation what are you trying to achieve?

from freezed.

valle-xyz avatar valle-xyz commented on June 17, 2024

Hi @elianmortega, thank you for your answer. I try to convert the Timestamp to a DateTime and vice versa. This is needed to work with the Timestamp from Firestore.

The Converter uses this code:

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

class TimestampConverter implements JsonConverter<DateTime, Timestamp> {
  const TimestampConverter();

  @override
  DateTime fromJson(Timestamp timestamp) {
    return timestamp.toDate();
  }

  @override
  Timestamp toJson(DateTime date) => Timestamp.fromDate(date);
}

But somehow freezer does not recognize it.

from freezed.

elianortega avatar elianortega commented on June 17, 2024

Thanks @Valentin-Seehausen, it's true I just try with different ways and freezed wasnt able to recognize the timestamp.

from freezed.

valle-xyz avatar valle-xyz commented on June 17, 2024

I ended up with this:

/// Converts DateTime to Timestamp for serialization of Firestore object
Timestamp dateTimeToJson(DateTime? dateTime) =>
    Timestamp.fromDate(dateTime ?? DateTime(2021));

/// Converts Timestamp to DateTime for serialization of Firestore object
DateTime? dateTimeFromJson(Timestamp? timestamp) =>
    timestamp?.toDate() ?? DateTime(2021);

class NewsPost with _$NewsPost {
  factory NewsPost({
    @JsonKey(name: 'date', fromJson: dateTimeFromJson, toJson: dateTimeToJson)
        DateTime? date,
  }) = _NewsPost;

  factory NewsPost.fromJson(Map<String, dynamic> json) =>
      _$NewsPostFromJson(json);
}

Not being happy with the default values obviously.

from freezed.

maks avatar maks commented on June 17, 2024

@Valentin-Seehausen which version of Freezed are you using? This works fine for me with freezed: ^0.14.1 and json_serializable: ^4.1.0:

@freezed
class UserDetails with _$UserDetails {
  factory UserDetails({
    required String firstName,
    required String lastName,
    @JsonKey(name: 'date')
    @CustomDateTimeConverter()
        required DateTime dateOfBirth,
    required String email,
    required String street,
    required String city,
    required String state,
  }) = _UserDetails;

  factory UserDetails.blank() => UserDetails(
        firstName: '',
        lastName: '',
        dateOfBirth: DateTime.fromMillisecondsSinceEpoch(0),
        email: '',
        street: '',
        city: '',
        state: '',
      );

  factory UserDetails.fromJson(Map<String, dynamic> json) =>
      _$UserDetailsFromJson(json);
}

class CustomDateTimeConverter implements JsonConverter<DateTime, String> {
  const CustomDateTimeConverter();

  @override
  DateTime fromJson(String json) {
    return DateTime.parse(json);
  }

  @override
  String toJson(DateTime dt) => DateFormat('yyyy-MM-dd').format(dt);
}

I see them copied over correctly to generated code:

...
/// @nodoc
mixin _$UserDetails {
  String get firstName => throw _privateConstructorUsedError;
  String get lastName => throw _privateConstructorUsedError;
  @JsonKey(name: 'date')
  @CustomDateTimeConverter()
  DateTime get dateOfBirth => throw _privateConstructorUsedError;
  String get email => throw _privateConstructorUsedError;
...

and then this test passes:

void main() {
  test('user details model serialises to expected date format', () async {
    final testUser = UserDetails.blank();

    final expectedJson =
        '{"firstName":"","lastName":"","date":"1970-01-01","email":"","street":"","city":"","state":""}';

    expect(json.encode(testUser.toJson()), expectedJson);
  });
}

from freezed.

valle-xyz avatar valle-xyz commented on June 17, 2024

I use
json_annotation: ^4.0.1
freezed_annotation: ^0.14.1

and as dev dependencies

freezed: ^0.14.1+2
json_serializable: ^4.1.0

I found a workaround, but still don't get why it is not working.

from freezed.

maks avatar maks commented on June 17, 2024

@Valentin-Seehausen the code I posted works for me. Could you elaborate on what is not working for you? Can you see the annotations copied across into the freezed generated file?

from freezed.

edgeboy47 avatar edgeboy47 commented on June 17, 2024

I'm not sure if this was already fixed, but I noticed that when the DateTime field is set as required, the TimeConverter works. But when the DateTime field is marked as nullable, it does not work.

from freezed.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.