【问题标题】:Getter 'Length' was called on null在 null 上调用了 Getter 'Length'
【发布时间】:2021-07-21 23:33:37
【问题描述】:

当我打开选项卡时,有一秒钟,出现此错误。然后它消失了,我该如何解决这个问题? 我假设生成长度值需要时间?

class rideList extends StatefulWidget {
  @override
  _rideListState createState() => _rideListState();
}

class _rideListState extends State<rideList> {
  @override
  Widget build(BuildContext context) {

    final rides = Provider.of<List<RideDetails>>(context);
    int count = 0;

    if (rides.length != null) {
      return ListView.builder(
        itemCount: rides.length,
        itemBuilder: (context, index) {
          print('Rides: ${rides.length}');
          if (rides[index].limit == true) {
            return RideTile(ride: rides[index]);
          }
          else if(rides[index].limit == false){
            ++count;
            if(rides.length == count){
              print('no rides');
              return Center(child: Text('No rides posted yet. Be the first to post a ride!'));
            }
            else{
              return SizedBox(height: 20.0,);
            }
          }
          return SizedBox(height: 20.0,);
        },
      );
    }
  }
}

【问题讨论】:

    标签: firebase flutter google-cloud-firestore


    【解决方案1】:

    如果rides 总是一个可以为空的 List(),

    你应该使用条件 => if (rides != null)

    而不是 => if (rides.length != null).

    发生错误是因为在调用属性length 之前,您必须先检查rides 是否为空。

    【讨论】:

      猜你喜欢
      • 2021-08-11
      • 2021-02-23
      • 2021-12-10
      • 2018-12-03
      • 2020-06-15
      • 2021-05-25
      • 2023-03-08
      • 1970-01-01
      相关资源
      最近更新 更多