Skip to content

Instantly share code, notes, and snippets.

@adash333
Created January 2, 2020 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adash333/00cff617f761e89a76eff2c3d2d42712 to your computer and use it in GitHub Desktop.
Save adash333/00cff617f761e89a76eff2c3d2d42712 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
import 'package:firebase/firebase.dart';
import 'package:firebase/firestore.dart' as fs;
void main() {
initializeApp(
apiKey: "YourApiKey",
authDomain: "YourAuthDomain",
databaseURL: "YourDatabaseUrl",
projectId: "YourProjectId",
storageBucket: "YourStorageBucket");
);
//fs.Firestore store = firestore();
//fs.CollectionReference ref = store.collection("kasikari-memo");
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
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) {
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
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']),
subtitle: Text(message['stuff']),
isThreeLine: true,
),
);
},
).toList(),
)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment