简体   繁体   中英

Error in ListView in the end of thel list in flutter

I am using the below widget and the CardItem class to fetch documents from the firestore database. Which is working fine. But When I reach the last document it shows the error as show in the this image图片 . How should I resolve it?

WIDGET

Widget build(BuildContext context) {
    return StreamBuilder<QuerySnapshot>(
      stream: widget._firestore.collection("posts").snapshots(),
      builder: (context,snapshot){
         //String itemTitle = snapshot.data.documents[index]['postContent'];

        if (!snapshot.hasData){
          return Text("Loading");
        }

        return ListView.builder(
            itemCount: snapshot.data.documents.length,
            itemBuilder: (context, index){
          String itemTitle = snapshot.data.documents[index]['postContent'];
          return CardItem(itemTitle:itemTitle);

        });
      },
    );

CARDITEM

class CardItem extends StatefulWidget {
  String itemTitle;
  CardItem({this.itemTitle});
  @override
  _CardItemState createState() => _CardItemState();
}

class _CardItemState extends State<CardItem> {
  bool ischecked = false;
  @override
  Widget build(BuildContext context) {
    return Card(

      child: ListTile(
        title: Text(widget.itemTitle),
      ),
    );
  }
}

Use Ternary Operator

Text(widget.itemTitle !=null ? widget.itemTitle: 'on null add any default description')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM