Created
January 4, 2020 03:23
-
-
Save adash333/8c4d8bf16f5ba92e6fda74e0bc98bc41 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'package:flutter/material.dart'; | |
| import 'package:firebase/firebase.dart'; | |
| import 'package:firebase/firestore.dart' as fs; | |
| // initializeApp()の中身は、ご自身の設定に書き換えてください。 | |
| void main() { | |
| initializeApp( | |
| apiKey: "YourApiKey", | |
| authDomain: "YourAuthDomain", | |
| databaseURL: "YourDatabaseUrl", | |
| projectId: "YourProjectId", | |
| storageBucket: "YourStorageBucket"); | |
| ); | |
| runApp(MyApp()); | |
| } | |
| class MyApp extends StatelessWidget { | |
| @override | |
| Widget build(BuildContext context) { | |
| return MaterialApp( | |
| title: 'Flutter Firestore memo', | |
| theme: ThemeData( | |
| primarySwatch: Colors.blue, | |
| ), | |
| home: MyHomePage(title: 'リスト表示'), | |
| ); | |
| } | |
| } | |
| class MyHomePage extends StatefulWidget { | |
| MyHomePage({Key key, this.title}) : super(key: key); | |
| final String title; | |
| @override | |
| _MyHomePageState createState() => _MyHomePageState(); | |
| } | |
| class _MyHomePageState extends State<MyHomePage> { | |
| final fs.Firestore store = firestore(); | |
| final List<Map<String, dynamic>> messages = List(); | |
| final List<Map<String, dynamic>> kasikarimemo = List(); | |
| fetchMessages() async { | |
| var messagesRef = await store.collection('messages').get(); | |
| var memosRef = await store.collection('kasikarimemo').get(); | |
| messagesRef.forEach( | |
| (doc) { | |
| messages.add(doc.data()); | |
| }, | |
| ); | |
| memosRef.forEach( | |
| (doc) { | |
| kasikarimemo.add(doc.data()); | |
| }, | |
| ); | |
| setState(() {}); | |
| } | |
| @override | |
| void initState() { | |
| super.initState(); | |
| fetchMessages(); | |
| } | |
| @override | |
| Widget build(BuildContext context) { | |
| return Scaffold( | |
| appBar: AppBar( | |
| title: Text(widget.title), | |
| ), | |
| floatingActionButton: FloatingActionButton( | |
| onPressed: () async { | |
| var m = Map<String, String>(); | |
| m['content'] = 'hogehoge'; | |
| await store.collection('messages').add(m); | |
| setState(() { | |
| messages.add(m); | |
| }); | |
| }, | |
| child: Icon(Icons.add), | |
| ), | |
| body: ListView( | |
| children: kasikarimemo.map( | |
| (message) { | |
| return Card( | |
| child: ListTile( | |
| leading: FlutterLogo(size: 72.0), | |
| // title: Text(message['borrowOrLend']), | |
| title: Text("【 " + (message['borrowOrLend'] == "lend"?"貸": "借") +" 】"+ message['stuff']), | |
| // subtitle: Text(message['stuff']), | |
| subtitle: Text('期限 : ' + message['date'].toString().substring(0,10) + "\n相手 : " + message['user']), | |
| isThreeLine: true, | |
| ), | |
| ); | |
| }, | |
| ).toList(), | |
| ) | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment