LoginSignup
6
5

More than 3 years have passed since last update.

【Flutter×Firebase】Firestoreのtimestampデータを取得したり、登録したりする

Posted at

こんにちは。

①FirestoreからTimestamp型のデータを取得して、画面に表示する
②画面に入力された日付を、Firestoreに登録する

これで少しつまづいたので、メモ程度に残しておきます。

FirestoreからTimestamp型のデータを取得して、画面に表示する

Firestoreに「created_at」というカラムでTimestamp型で登録している場合。

ポイント

  • 一回、Date型に変換してから文字列に変換する

例はこんな感じです

このコードでは例えば「2020/07/24」という文字が出力されます。

Text(
  DateFormat("yyyy/MM/dd").format(_when.toDate()).toString(),
  style: TextStyle(
    fontSize: 16.0,
    color: Colors.black
  ),
),

FirestoreにTimestamp型のデータを登録する

ポイント

  • DateTime型のまま登録してOK

変数「created_at」にDateTime型を入れているとして...

Firestore.instance.collection("activelists").add({
  "created_at": _created_at,
});

終わりに

FirestoreのTimestamp型の扱い方が分からなかったので、色々調べながら扱えるようになりました。

つまづいた方はぜひ参考にしてみてください。

6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5