Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Flutter ListView Not Displaying Items When Wrapped Without A Sized Box

I have implemented a Card containing a Stream-Builder that has a List-View which displays items.(Items are present and display is okay). However the Card won’t display items unless I wrap it within a Sized-Box with specific height. Fixed height isn’t ideal as I want the text to wrap content since I can’t tell what the length of the dynamic text will be. Without the sized-box with fixed height items just don’t appear at all.

Have tried wrapping the Card with Flexible, Wrap, Column and Row but in all these items just won’t display. How can I fix this?

        Card(
            child: StreamBuilder(
              stream: _midref.onValue,
              builder: (context, AsyncSnapshot snapshot) {
                if (snapshot.hasData &&
                    !snapshot.hasError &&
                    snapshot.data.snapshot.value != null) {
                  midlists.clear();

                  DataSnapshot dataValues = snapshot.data.snapshot;
                  Map<dynamic, dynamic> values =
                  dataValues.value as Map;
                  values.forEach((key, values) {
                    midlists.add(values);
                  });

                  return ListView.builder(
                    scrollDirection: Axis.horizontal,
                    itemCount: midlists.length,
                    itemBuilder: (BuildContext context, int index) {
                      return Card(
                        margin: EdgeInsets.fromLTRB(2, 2, 2, 2),
                        elevation: 20,
                        child: Text(
                          midlists[index]["name"].toUpperCase(),
                          textAlign: TextAlign.center,
                          style: TextStyle(fontSize: 13),
                        ),
                      );
                    },
                  );
                }

                return Container();
              },
            ),
          ),

Parent Container. This Container is wrapped inside a Single Child ScrollView

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

                         Column(
                                mainAxisAlignment: MainAxisAlignment.start,
                                crossAxisAlignment: CrossAxisAlignment.stretch,
                                children: [
                                
                                  ]),

>Solution :

wrap your card With Expanded like:

 Expanded(child:  Card(
        child: StreamBuilder(
          stream: _midref.onValue,
          builder: (context, AsyncSnapshot snapshot) {
            if (snapshot.hasData &&
                !snapshot.hasError &&
                snapshot.data.snapshot.value != null) {
              midlists.clear();

              DataSnapshot dataValues = snapshot.data.snapshot;
              Map<dynamic, dynamic> values =
              dataValues.value as Map;
              values.forEach((key, values) {
                midlists.add(values);
              });

              return ListView.builder(
                scrollDirection: Axis.horizontal,
                itemCount: midlists.length,
                itemBuilder: (BuildContext context, int index) {
                  return Card(
                    margin: EdgeInsets.fromLTRB(2, 2, 2, 2),
                    elevation: 20,
                    child: Text(
                      midlists[index]["name"].toUpperCase(),
                      textAlign: TextAlign.center,
                      style: TextStyle(fontSize: 13),
                    ),
                  );
                },
              );
            }

            return Container();
          },
        ),
      ));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading