0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Flutter Firestoreからドキュメントのフィールドを取得する

Posted at

前提

FlutterとFirebaseとの連携パートは省いています!

バージョンは下記の通り

pubspec.yaml
dependencies:
  flutter:
    sdk: flutter
  firebase_core: "0.7.0"
  cloud_firestore: "^0.16.0+1"

ドキュメントのフィールドとは?

スクリーンショット 2021-05-14 10.01.43.jpg

赤丸で囲った部分のことです。
コレクション>ドキュメント>フィールドという具合にデータが格納されているイメージ

Firestoreからドキュメントのフィールドを取得する

import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:cloud_firestore/cloud_firestore.dart';

// 省略

 fetchUserData() async {
    final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
    final FirebaseFirestore _firestore = FirebaseFirestore.instance;
    final userId = _firebaseAuth.currentUser.uid;
    DocumentSnapshot snapshot = await _firestore.doc('users/${userId}').get();
    print(snapshot.data()['email']);
    return snapshot.data();
  }

 @override
  Widget build(BuildContext context) {
// 省略
    TextButton(
      onPressed: fetchUserData, child: Text('取得!')),
}

上記のコードの場合、emailのみ取得します。

print(snapshot.data());

上記のコードの場合、全てのフィールドを取得します

こういうことって、何気に公式ドキュメントを読んでもわからなかったりするので。。。。

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?