M1 MacBook、Android Studio、Flutter 2.0
2021年1月末からプログラミング学習始めた初心者です。自分用メモ。
1、FlutterとFirebaseをリンク
https://firebase.google.com/docs/flutter/setup
2、最初のおまじない
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
3、以下、CRUD処理
・登録(Create)
var collection = FirebaseFirestore.instance.collection("sauna");
collection.add({
'title': title,
'isDone': false,
'createdTime': Timestamp.now()
});
読取(Read)
StreamBuilder<QuerySnapshot>(
stream: saunas.snapshots(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return ListView.builder(
itemBuilder: (BuildContext context, int index){
if(snapshot.data!.docs[index]['isDone']) return Container();
return CheckboxListTile(
controlAffinity: ListTileControlAffinity.leading,
title: Text(snapshot.data!.docs[index]['title']),
value: snapshot.data!.docs[index]['isDone'],
更新(Update)
onPressed: ()async{
await snapshot.data!.docs[index].reference.update({
'title': editTitleController.text,
'updatedTime': Timestamp.now(),
});
Navigator.pop(context);
},
child: const Text('編集'),
削除(Delete)
onPressed: ()async{
await snapshot.data!.docs[index].reference.delete();
Navigator.pop(context);
},
child: Text('削除実行'),
),