0
3

More than 3 years have passed since last update.

Flutter×FirebaseのCRUD処理まとめ

Last updated at Posted at 2021-06-17

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('削除実行'),
            ),
0
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
3