【问题标题】:Flutter) Using StreamBuilder is too difficult help me senpaiFlutter)使用StreamBuilder太难了,前辈帮帮我
【发布时间】:2023-02-04 20:25:58
【问题描述】:

我现在正在使用 flutter,而 StreamBuilder 让我很生气......

我认为 ListView.builder 有问题,我用容器包裹它并改变了高度,还有 Expanded,但它没有用。我使用 firebase 作为数据库。什么问题?????

 body: SingleChildScrollView(
        scrollDirection: Axis.vertical,
        child: Container(
          color: Color(0xffe9ecef),
          height: 2000,
          child: StreamBuilder(
            stream: content.snapshots(),
            builder: (context , snapshot)
            {if (snapshot.hasData){return Expanded(child:ListView.builder(
              itemCount: snapshot.data!.docs.length,
              itemBuilder: (context, index) 

               { DocumentSnapshot document = snapshot.data!.docs[index];
                 return Text(document['text']);}
            ));}else{return CircularProgressIndicator();}}
)
        )
      )

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    问题是您已经在顶部 [SingleChildScrollView] 上有了可滚动的小部件。因此将 ListView 放入其中会导致渲染问题。

    您可以改用 Column

    body: SingleChildScrollView(
        scrollDirection: Axis.vertical,
        child: Container(
          color: Color(0xffe9ecef),
          height: 2000,
          child: StreamBuilder(
            stream: content.snapshots(),
            builder: (context, snapshot) {
              if (snapshot.hasData) {
                return Column(
                    children:
                        List.generate(snapshot.data?.docs.length ?? 0, (index) {
                  DocumentSnapshot document = snapshot.data!.docs[index];
                  return Text(document['text']);
                }));
              } else {
                return CircularProgressIndicator();
              }
            },
          ),
        ),
      ),
    

    或者更好地使用body:ListView

    Scaffold(
      backgroundColor: Color(0xffe9ecef),
      body: StreamBuilder(
        stream: content.snapshots(),
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            return ListView.builder(
              itemCount: snapshot.data!.docs.length,
              itemBuilder: (context, index) {
                DocumentSnapshot document = snapshot.data!.docs[index];
                return Text(document['text']);
              },
            );
          } else {
            return CircularProgressIndicator();
          }
        },
      ),
    ),
    

    【讨论】:

      猜你喜欢
      • 2020-10-13
      • 1970-01-01
      • 2011-01-08
      • 1970-01-01
      • 2016-12-28
      • 2017-08-25
      • 1970-01-01
      • 2011-08-22
      • 1970-01-01
      相关资源
      最近更新 更多