1
0

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 1 year has passed since last update.

[Flutter]StreamBuilderとFutureBuilderの違いについて

Last updated at Posted at 2022-08-06

大枠
・StreamBuilder・・・リアムタイムに更新する値を再描画なしで反映する
・FutureBuilder・・・一度のみ値を取得し、リアムタイムの変更を反映しない

[FutureBuilder]

class HomeScreen extends StatelessWidget {
  final Future<QuerySnapshot> _wordStream =
    FirebaseFirestore.instance.collection('cities').get();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Memo Pad'),
      ),
      body: FutureBuilder(
        future: _wordStream,
        builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
          if (snapshot.hasData) {
            return ListView(
              children: snapshot.data!.docs.map((DocumentSnapshot document) {
                return ListTile(
                  title: Text(document['name']),
                );
              }).toList(),
            );
          } else {
            return Container(
              child: CircularProgressIndicator(),
            );
          }
        },
      ),
    );

[StreamBuilder]

class HomeScreen extends StatelessWidget {
  final Stream<QuerySnapshot> _wordStream =
     FirebaseFirestore.instance.collection('cities').snapshots();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Memo Pad'),
      ),
      body: StreamBuilder(
        stream: _wordStream,
        builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
          if (snapshot.hasData) {
            return ListView(
              children: snapshot.data!.docs.map((DocumentSnapshot document) {
                return ListTile(
                  title: Text(document['name']),
                );
              }).toList(),
            );
          } else {
            return Container(
              child: CircularProgressIndicator(),
            );
          }
        },
      ),
    );
  }
}
1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?