見出し画像

flutter - [Firebase Database timestamp] を、 [flutter - DateTime型] で受ける

まあ非常に単純な話ですが私が二度手間食らいたくないだけです
toDate() つけるだけです。

サンプル

final db = FirebaseFirestore.instance;
final collection = db.collection('コレクション名');
final querySnapshot = await collection.get();
for (final doc in querySnapshot.docs) {
    _listname.add(Content(
        id : doc['id'],
        createdDate: doc['createdDate'].toDate()   //👈これ
    ));
}

Content は id と createdDate という項目があって
これが DateTime で定義されている場合、Firebase からとってきてそのまま格納できないので toDate() をつけましょうね。ということ。
それだけ。以上。

出てしまうエラーはこんな感じ。

E/flutter (17972): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type 'Timestamp' is not a subtype of type 'DateTime'


そのまんまですね。

この記事が気に入ったらサポートをしてみませんか?