【问题标题】:Error in ListView in the end of thel list in flutter颤动中列表末尾的ListView错误
【发布时间】:2020-05-26 04:00:24
【问题描述】:

我正在使用以下小部件和 CardItem 类从 firestore 数据库中获取文档。哪个工作正常。但是当我到达最后一个文档时,它会显示此图像 中所示的错误。我该如何解决?

小工具

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),
      ),
    );
  }
}

【问题讨论】:

  • 错误信息本身就是解决方案。对于列表中的最后一个 CardItem,“itemTitle”为空。要进行检查,请在 Text 小部件中添加一个可识别 null 的条件,如下所示:Text(widget.itemTitle ?? 'NULL PASSED')
  • @Thepeanut 感谢您解决了我的问题。我是颤振开发的新手。很快就会好起来的
  • 您仍可能需要检查 snapshot.data.documents 的完整响应,以更好地理解为什么 postContent 为空(如果它不应该是)

标签: flutter google-cloud-firestore flutter-listview


【解决方案1】:

使用三元运算符

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

【讨论】:

    猜你喜欢
    • 2011-04-06
    • 1970-01-01
    • 2013-08-18
    • 2023-01-17
    • 1970-01-01
    • 2020-06-24
    • 1970-01-01
    • 1970-01-01
    • 2019-09-15
    相关资源
    最近更新 更多