0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

FlutterでStreamBuilder、FutureBuilder、ListView.builder、ListTileの3段論法みたいなやつの書き方

Last updated at Posted at 2021-09-29

FlutterでStreamBuilder、FutureBuilder、ListView.builder、ListTileの3段論法みたいなやつの書き方

・ListView.builderでListTileを量産、itemCountにアイテムの個数、itemBuilderにListTileを指定
・FutureBuilderでListView.builderを監視、futureが変化した際にbuilder以下が発動
・StreamBuilderでFutureBuilderを監視、streamが変化した際にbuilder以下が発動
この3段論法みたいなのを自力でカッコよく書けるようになりたい

StreamBuilder<QuerySnapshot>(
  stream: FirebaseFirestore.instance.collection('users').snapshots(),
  builder: (context, snapshot) {
    return FutureBuilder<>(
      future: FirebaseFirestore.instance.collection('messages').snapshot(),
      builder: (context, snapshot){
        return ListView.builder(
          itemCount: snapshot.data!.length,
          itemBuilder: (context, index){
            return ListTile(
              leading: Icon(Icons.home)
              title: Text(snapshot.data![index].title),
              trailing: Icon(Icons.settings)
              onTap: () {
                // Do something
                },
            )
          }
        )
      }
    )
  }
)

公式ドキュメント:
https://api.flutter.dev/flutter/widgets/StreamBuilder-class.html
https://api.flutter.dev/flutter/widgets/FutureBuilder-class.html
https://api.flutter.dev/flutter/widgets/ListView-class.html
https://api.flutter.dev/flutter/material/ListTile-class.html

参考:
https://bukiyo-papa.com/streambuilder
https://zenn.dev/sqer/articles/db20a4d735fb7e5928ba
https://ryamamoto.netlify.app/2020/01/16/future-future-stream

0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?