How to convert Firestore DocumentSnapshot to dart object instance

user93796

I have a class as follows which i need to read/write to/from FireStore.

class Customer{
   String fname;
   String fname;

   Map<String, dynamic> toJson() => {
    'fname': fname,
    'lname': lname
  };

  static Customer fromDocument(DocumentSnapshot doc){
    Customer customer = Customer();
    customer.fname = doc.data['fname'];
    customer.lname = doc.data['lname'];
    return customer;
  }
}

How can I avoid writing code to serialize(toJson) /deserilize (fromDocument)? For amazon if i have to do this in java i would use Gson library to serialize and deserialize.

Kirill Bubochkin

You can use json_serializable package, that generates code for serializing/deserializing. With it your code will look like that:

@JsonSerializable()
class Customer {
  Customer({this.fname, this.lname});

  final String fname;
  final String lname;

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

  static Customer fromJson(Map<String, dynamic> json) =>
      _$CustomerFromJson(json);
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Convert Firestore DocumentSnapshot to Map in Flutter

分類Dev

Firestore iOS DocumentSnapshot `createTime`

分類Dev

FireStore fromSnapshot vs fromMap for reading DocumentSnapshot?

分類Dev

In Dart how to get the ID of clicked target object?

分類Dev

How to save a `DateTime` instance of Dart to sqlite `timestamp` field?

分類Dev

How do you convert an instance of generic T into a concrete instance in Rust?

分類Dev

How to convert an object to JSON in Angular

分類Dev

How to convert array to object in Javascript?

分類Dev

How to convert object into string in javascript?

分類Dev

How To Convert IEnumerable To one object

分類Dev

How to convert a String to Object in Flutter?

分類Dev

How to identify an instance of multidimensional object in java?

分類Dev

How to print out instance of object that is stored in an array

分類Dev

How to convert From DateTime to unix timestamp in Flutter or Dart in general

分類Dev

How to convert Uint8list to List<int> with dart?

分類Dev

How do I convert a Firestore date/Timestamp to Date in Kotlin?

分類Dev

documentSnapshotからCloud Firestoreドキュメント参照を取得する

分類Dev

FirestoreはDocumentSnapshotのフィールドの値を取得します

分類Dev

未定義のdocumentSnapshot.createTimeを返すFirestore(javascriptでwebprojectを使用)

分類Dev

未定義のdocumentSnapshot.createTimeを返すFirestore(javascriptでwebprojectを使用)

分類Dev

How to convert an array into an object using stdClass()

分類Dev

How will i convert json string to Json Object

分類Dev

How do I convert a string to an object

分類Dev

How to convert the WebElement to RepoItemInfo object in Ranorex

分類Dev

How to convert a Python dict to a Class object

分類Dev

How to convert HashMap<Object, int[]> values to a matrix

分類Dev

How to convert query string to multi level object

分類Dev

How to convert query result object into string in CodeIgniter

分類Dev

How to convert JSON bundle to JSON Object in android

Related 関連記事

  1. 1

    Convert Firestore DocumentSnapshot to Map in Flutter

  2. 2

    Firestore iOS DocumentSnapshot `createTime`

  3. 3

    FireStore fromSnapshot vs fromMap for reading DocumentSnapshot?

  4. 4

    In Dart how to get the ID of clicked target object?

  5. 5

    How to save a `DateTime` instance of Dart to sqlite `timestamp` field?

  6. 6

    How do you convert an instance of generic T into a concrete instance in Rust?

  7. 7

    How to convert an object to JSON in Angular

  8. 8

    How to convert array to object in Javascript?

  9. 9

    How to convert object into string in javascript?

  10. 10

    How To Convert IEnumerable To one object

  11. 11

    How to convert a String to Object in Flutter?

  12. 12

    How to identify an instance of multidimensional object in java?

  13. 13

    How to print out instance of object that is stored in an array

  14. 14

    How to convert From DateTime to unix timestamp in Flutter or Dart in general

  15. 15

    How to convert Uint8list to List<int> with dart?

  16. 16

    How do I convert a Firestore date/Timestamp to Date in Kotlin?

  17. 17

    documentSnapshotからCloud Firestoreドキュメント参照を取得する

  18. 18

    FirestoreはDocumentSnapshotのフィールドの値を取得します

  19. 19

    未定義のdocumentSnapshot.createTimeを返すFirestore(javascriptでwebprojectを使用)

  20. 20

    未定義のdocumentSnapshot.createTimeを返すFirestore(javascriptでwebprojectを使用)

  21. 21

    How to convert an array into an object using stdClass()

  22. 22

    How will i convert json string to Json Object

  23. 23

    How do I convert a string to an object

  24. 24

    How to convert the WebElement to RepoItemInfo object in Ranorex

  25. 25

    How to convert a Python dict to a Class object

  26. 26

    How to convert HashMap<Object, int[]> values to a matrix

  27. 27

    How to convert query string to multi level object

  28. 28

    How to convert query result object into string in CodeIgniter

  29. 29

    How to convert JSON bundle to JSON Object in android

ホットタグ

アーカイブ