| // Copyright 2017, the Chromium project authors. Please see the AUTHORS file |
| // for details. All rights reserved. Use of this source code is governed by a |
| // BSD-style license that can be found in the LICENSE file. |
| |
| import 'dart:async'; |
| |
| import 'package:flutter/material.dart'; |
| import 'package:cloud_firestore/cloud_firestore.dart'; |
| |
| void main() { |
| runApp(new MaterialApp(title: 'Firestore Example', home: new MyHomePage())); |
| } |
| |
| class BookList extends StatelessWidget { |
| @override |
| Widget build(BuildContext context) { |
| return new StreamBuilder<QuerySnapshot>( |
| stream: Firestore.instance.collection('books').snapshots, |
| builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) { |
| if (!snapshot.hasData) return const Text('Loading...'); |
| return new ListView( |
| children: snapshot.data.documents.map((DocumentSnapshot document) { |
| return new ListTile( |
| title: new Text(document['message'] ?? '<No message retrieved>'), |
| ); |
| }).toList(), |
| ); |
| }, |
| ); |
| } |
| } |
| |
| class MyHomePage extends StatelessWidget { |
| CollectionReference get messages => Firestore.instance.collection('messages'); |
| |
| Future<Null> _addMessage() async { |
| Firestore.instance |
| .collection('books') |
| .document() |
| .setData(<String, String>{'message': 'Hello world!'}); |
| } |
| |
| @override |
| Widget build(BuildContext context) { |
| return new Scaffold( |
| appBar: new AppBar( |
| title: const Text('Firestore Example'), |
| ), |
| body: new BookList(), |
| floatingActionButton: new FloatingActionButton( |
| onPressed: _addMessage, |
| tooltip: 'Increment', |
| child: new Icon(Icons.add), |
| ), |
| ); |
| } |
| } |