Search code examples
flutterlistviewdartbuilder

How to show list of Strings in Listview in flutter


I have list of strings.I need to show them in interface as list.

List<String> spec_list = urlremoved.split(", ");

I need to just show them.but it is not working.please help met o findout the error. I have attached the code below

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';



class ItemView extends StatefulWidget {
  final String docID123;

  const ItemView({ this.docID123}) ;

  @override
  _ItemViewState createState() => _ItemViewState();
}
String name123;
class _ItemViewState extends State<ItemView> {
  @override
  //String docuID = Widget.documentid;
  
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Item Data'),
      ),
      body: ListView(
        children: <Widget>[
          Text(widget.docID123),
          getItems(),
          
        ],
      ),
    );
  }
}


Widget getItems(){
    return StreamBuilder(
      //1597917013710
      stream: Firestore.instance.collection('ads').document('1597917013710').snapshots(),
      builder: (context, snapshot){
        if (!snapshot.hasData) {
          //snapshot.data.toString();
          return new Text("Loading");
        }
        DocumentSnapshot dd = snapshot.data; 

        var userDocument = snapshot.data;
        //String myname = dd.data.toString();
        int len = dd.data.length;
        
        String jsonString = dd.data.toString();
        String start = "[";
        String end = "]";
        final startIndex = jsonString.indexOf(start);
        final endIndex = jsonString.indexOf(end, startIndex + start.length);
        String next = jsonString.substring(startIndex + start.length, endIndex);
        String imagelinkRemoved = jsonString.replaceAll(next, "");
        String urlremoved = imagelinkRemoved.replaceAll("urls: [], ", "").replaceAll("{", "").replaceAll("}", "");

        List<String> spec_list = urlremoved.split(", ");
        int speclistlen = spec_list.length;
        
        return Container(
      child: Column(
        children: <Widget>[
          Text(spec_list[0]),
          ListView.builder(
            itemCount: speclistlen,
            itemBuilder: (context,index){
              return Text(spec_list[index]);
            },
          )
        ],
      ),
    );
      }
    );
}


Future getCatagory() async {
      var firestone = Firestore.instance;

      QuerySnapshot alldata = await firestone.collection("catagory_names/Vehicles").getDocuments();
      for(int i=0;i<alldata.documents.length;i++){
                        DocumentSnapshot snap = alldata.documents[i];
                        
                      }

      return  Text('12345');

    }

Solution

  • This can only happen if your speclistlen is empty While following above code it seems that you are getting unbound height exception. To avoid that use shrinkWrap: true, inside Listview.Builder

    Widget getItems() {
      return StreamBuilder(
          //1597917013710
          stream: Stream.periodic(Duration(seconds: 2)),
          builder: (context, snapshot) {
            String urlremoved = "I, am, jits555";
    
            List<String> spec_list = urlremoved.split(", ");
            int speclistlen = spec_list.length;
    
            return Container(
              child: Column(
                children: <Widget>[
                  Text(spec_list[0]),
                  ListView.builder(
                    shrinkWrap: true,
                    itemCount: speclistlen,
                    itemBuilder: (context, index) {
                      return Text(spec_list[index]);
                    },
                  )
                ],
              ),
            );
          });
    }