只有一个数据项来自 Firebase Firestore。

huangapple go评论69阅读模式
英文:

Only one item of data is coming from Firebase Firestore

问题

我写了这样的代码:

Container(
  height: 250,
  child: StreamBuilder(
    stream: db.collection("DebrisPeoples").snapshots(),
    builder: (context, snapshot) {
      if (!snapshot.hasData) {
        return Text("加载中");
      } else {
        final List<DebrisPeopleModel> data = snapshot.data!.docs
            .map((e) => DebrisPeopleModel.fromDocument(e))
            .toList();

        inspect(data);
        /*
          输出:
          [0]: DebrisPeopleModel
          [1]: DebrisPeopleModel
          [2]: DebrisPeopleModel
          [3]: DebrisPeopleModel
          [4]: DebrisPeopleModel
          [5]: DebrisPeopleModel
        */
        for (var item in data)
          return Card(
            child: ListTile(
              title: Text(item.nameSurname.toString()),
            ),
          );
      }
      return Container();
    },
  ),
),

当我将数据打印到控制台时,数据显示出来,但当我将其投影到屏幕时,只有一个数据项显示。这可能是因为仅在for循环的第一次迭代中返回了Card小部件,导致只显示一个数据项。您可能需要将返回Card的逻辑移动到循环之外,以便将所有数据项都包含在Container中。如果需要显示所有数据项,您可以尝试以下更改:

return Card(
  child: Column(
    children: data.map((item) {
      return ListTile(
        title: Text(item.nameSurname.toString()),
      );
    }).toList(),
  ),
);

这将在一个Card小部件中显示所有数据项。

英文:

I wrote code like this:

  Container(
    height: 250,
    child: StreamBuilder(
      stream: db.collection(&quot;DebrisPeoples&quot;).snapshots(),
      builder: (context, snapshot) {
        if (!snapshot.hasData) {
          return Text(&quot;y&#252;kleniyor&quot;);
        } else {
          final List&lt;DebrisPeopleModel&gt; data = snapshot.data!.docs
              .map((e) =&gt; DebrisPeopleModel.fromDocument(e))
              .toList();

          inspect(data);
          /*
             OUTPUT:
              [0]: DebrisPeopleModel
              [1]: DebrisPeopleModel
              [2]: DebrisPeopleModel
              [3]: DebrisPeopleModel
              [4]: DebrisPeopleModel
              [5]: DebrisPeopleModel
          */
          for (var item in data)
            return Card(
              child: ListTile(
                title: Text(item.nameSurname.toString()),
              ),
            );
        }
        return Container();
      },
    ),
  ),

When I print it to the console, data shows up, but when I project it to the screen, only one item of data appears. Why could this be?

答案1

得分: 1

使用ListView.builderCard包装起来,而不是使用for循环

英文:

Wrap the Card with a ListView.builder instead of using the for loop.

答案2

得分: 1

尝试返回所有卡片,而不是单个项目,如下所示:

 Container(
    //height: 250,
    child: StreamBuilder(
      stream: db.collection("DebrisPeoples").snapshots(),
      builder: (context, snapshot) {
        if (!snapshot.hasData) {
          return Text("加载中");
        } else {
         final List<DebrisPeopleModel> data = snapshot.data!.docs
              .map((e) => DebrisPeopleModel.fromDocument(e))
              .toList();
              
          inspect(data);
          List<Widget> cards = [];
          for (var item in data) {
            final card = Card(
              child: ListTile(
                title: Text(item.nameSurname.toString()),
              ),
            );
            cards.add(card);
          }
          return Column(children: cards); // 可能是ListView
        }
      },
    ),
  )
英文:

Try to return all cards instead of single item like

 Container(
    //height: 250,
    child: StreamBuilder(
      stream: db.collection(&quot;DebrisPeoples&quot;).snapshots(),
      builder: (context, snapshot) {
        if (!snapshot.hasData) {
          return Text(&quot;y&#252;kleniyor&quot;);
        } else {
         final List&lt;DebrisPeopleModel&gt; data = snapshot.data!.docs
              .map((e) =&gt; DebrisPeopleModel.fromDocument(e))
              .toList();
              
          inspect(data);
          List&lt;Widget&gt; cards = [];
          for (var item in data) {
            final card = Card(
              child: ListTile(
                title: Text(item.nameSurname.toString()),
              ),
            );
            cards.add(card);
          }
          return Column(children: cards); // perhaps ListView
        }
      },
    ),
  )

huangapple
  • 本文由 发表于 2023年2月14日 00:06:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75438384.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定