【Flutter】Firebaseの利用

firebaseのtoolkitをインストール
$ curl -sL https://firebase.tools | bash

$ flutter pub upgrade firebase_core
$ flutter pub add cloud_firestore
$ flutter pub upgrade cloud_firestore

1.firebaseで新しいプロジェクトを作成する
2.firestoreで新しいデータベースを作成する
3.コレクションの作成
name(string), mail(string), age(number)
4.firebaseの設定を行う
$ dart pub global activate flutterfire_cli
$ flutterfire configure

import 'package:flutter/material.dart';
import 'dart:ui' as ui;
import 'dart:convert';
import 'dart:io';
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';
import 'package:cloud_firestore/cloud_firestore.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );
  runApp(new MyApp());
}

// 省略

  void fire() async {
    FirebaseFirestore firestore = FirebaseFirestore.instance;
    final snapshot = await firestore.collection('mydata').get();
    var msg = '';
    snapshot.docChanges.forEach((element) {
      final name = element.doc.get('name');
      final mail = element.doc.get('mail');
      final age = element.doc.get('age');
      msg += "${name} (${age}) <${mail}>\n";
    });
    _controller.text = msg;
  }