skip to Main Content

I wrote code like this:

  Container(
    height: 250,
    child: StreamBuilder(
      stream: db.collection("DebrisPeoples").snapshots(),
      builder: (context, snapshot) {
        if (!snapshot.hasData) {
          return Text("yükleniyor");
        } else {
          final List<DebrisPeopleModel> data = snapshot.data!.docs
              .map((e) => 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?

2

Answers


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

    Login or Signup to reply.
  2. Try to return all cards instead of single item like

     Container(
        //height: 250,
        child: StreamBuilder(
          stream: db.collection("DebrisPeoples").snapshots(),
          builder: (context, snapshot) {
            if (!snapshot.hasData) {
              return Text("yükleniyor");
            } 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); // perhaps ListView
            }
          },
        ),
      )
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search