<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by fasunna on Medium]]></title>
        <description><![CDATA[Stories by fasunna on Medium]]></description>
        <link>https://medium.com/@fasunna?source=rss-187e7b98bf72------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*8V-XIHVRZab3x_ntvH23Gw.png</url>
            <title>Stories by fasunna on Medium</title>
            <link>https://medium.com/@fasunna?source=rss-187e7b98bf72------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 21:32:02 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@fasunna/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Firestore with Flutter JSON Annotation]]></title>
            <link>https://medium.com/@fasunna/firestore-with-flutter-json-annotation-69086e9099d?source=rss-187e7b98bf72------2</link>
            <guid isPermaLink="false">https://medium.com/p/69086e9099d</guid>
            <category><![CDATA[firestore]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[json]]></category>
            <category><![CDATA[annotations]]></category>
            <category><![CDATA[firebase]]></category>
            <dc:creator><![CDATA[fasunna]]></dc:creator>
            <pubDate>Fri, 07 Aug 2020 03:37:08 GMT</pubDate>
            <atom:updated>2020-08-07T13:41:15.256Z</atom:updated>
            <content:encoded><![CDATA[<p>Since Firebase and Flutter belong to Google company, one could think that should be easy to manipulate the data from one platform to another, right? Well… not so easy as one could desire, but we can achieve it with some tweaking.</p><p>In a recent Flutter project, I was trying to reduce as much as possible the struggle to transform a data structure from and to the Firebase SDK instances in the requests to my database. So I thought, why not using some annotation framework that should reduce a lot the boilerplate in my code? Dart for its own provides methods to transform from and to JSON, but if your class depends on another class that is private and doesn’t offer a serialization/deserialization you are pretty much stuck writing new code for each single one of them. So digging on the internet for a while a found an excellent framework for Flutter called json_annotation (which also is provided from Google developers) that cames with code generation features. Awesome!!! Right?!?! Unfortunately, not so there yet =/</p><p>Here is the thing, it works like a charm if you are using only the standard data types, such as string, number, boolean, array and object/map, but when it comes to the “reference” or even “timestamp” data types, Flutter and Firestore doesn’t communicate with each other that well. But there are some ways to take it around, reducing significantly the quantity of code to write.</p><h3><strong>First things first</strong></h3><p>If you are not familiar yet with both libraries, here a little introduction. If you already know some of its features, feel free to jump this little section.</p><p><a href="https://github.com/google/json_serializable.dart">JSON Annotation</a> library works like Java or even JavaScript libraries which can use annotation above classes, methods, or variables to help generate some code or even evaluate what is going on with it. The <a href="https://github.com/google/json_serializable.dart">JSON Annotation</a> for Flutter works exactly like that. It provides an annotator that can be placed at the top of the class to generate code to transform the data into a map of keys and values, which can be directly transformed into JSON.</p><p>Example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0cf77b20e2657db5798bbef71b776eda/href">https://medium.com/media/0cf77b20e2657db5798bbef71b776eda/href</a></iframe><p>With the code above, the code generator is able to create methods to transform the variables “name” and “age” from and to JSON, knowing exactly which data types each one is. To automate the code generation it is necessary to install as a development dependency the JSON serialization library and run the following command:</p><pre>flutter packages pub run build_runner watch — delete-conflicting-outputs</pre><p>It will generate an example.g.dart file which will have the code needed from Flutter to transform the data. For more information on how to use it, I recommend checking the examples in the <a href="https://github.com/google/json_serializable.dart">GitHub</a> repository of the libraries.</p><h3><strong>The</strong> <strong>problem</strong></h3><p>Ok, it works like intended when it is simple data types such as strings and numbers, but what is the problem again?</p><p>If you need some complex data management in the Firestore, it can be necessary to use some kind of references between data documents. This can be easily using strings, but it doesn’t feel right since you are losing a great feature of the Firestore support which is the “reference” data type. Or even if you need to transform the Timestamp format to Flutter’s DateTime it can be a little tedious to write it for every class you have.</p><p>Firebase SDK for Flutter uses custom classes to perform its operations for some data types, such as DocumentReference for reference data type and Timestamp for time and date formats. These classes don’t provide serialization methods, thus it is up to the developer to carefully use them. If you use a simple string instead of the DocumentReference when uploading the data to the Firestore, you could be a little disappointed to see all your work falling apart, since it will replace the reference data type for a string data type. It could be fine for some situations, but it necessary to be aware of it!</p><h3><strong>Somehow some solution</strong></h3><p>If you are trying to serialize data from another library, or even change the way the JSON is generated there are some conditions that can be provided in the annotations. Such as:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/505ff6f0a3fb94916b514f440007e895/href">https://medium.com/media/505ff6f0a3fb94916b514f440007e895/href</a></iframe><p>Adding the @JsonKey in the variables and setting the name for each of them, it is possible to change the serialization method, returning a key/value with different names that were declared in the class. Along with it, it is possible to provide custom methods for serialization/deserialization with “fromJson” and “toJson” parameters. So if we can provide a customizable serialization method in the annotation the code generation could do the rest of the work to us!</p><h3><strong>Final</strong> <strong>solution</strong></h3><p>So, long story short, to be able to send and retrieve the custom data types from and to Firestore, the best solution I could find is to provide a simple function in the desired variables.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2ec022d77da6a37ba3e69c7da0706543/href">https://medium.com/media/2ec022d77da6a37ba3e69c7da0706543/href</a></iframe><p>How to use it with Firebase SDK:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0e4d56eb2a06576dd00b63749fe134de/href">https://medium.com/media/0e4d56eb2a06576dd00b63749fe134de/href</a></iframe><p>It is no waterproof solution after all but could help you not getting so sweaty of writing so many boilerplate codes. I hope this could help some of you guys out there.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=69086e9099d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>