firebase - Flutter / Firebase在null上调用了getter'length'
问题描述
我使用 Firebase 作为后端开发 Flutter 应用程序,当我从屏幕移动到另一个 BottomNavigationBar 时,我正在使用 StreamProvider 将配置文件数据传递到另一个屏幕。
当我移动到另一个屏幕时,我收到此错误:
The getter 'length' was called on null.
Receiver: null
Tried calling: length
The relevant error-causing widget was:
ProfileList
这是 ProfileList 和 ProfileTile
class ProfileList extends StatefulWidget {
@override
_ProfileListState createState() => _ProfileListState();
}
class _ProfileListState extends State<ProfileList> {
@override
Widget build(BuildContext context) {
final profiles = Provider.of<List<Profile>>(context);
return ListView.builder(
itemCount: profiles.length,
itemBuilder: (context, index){
return ProfileTile(profile: profiles[index]);
});
}
}
ProfileTile
class ProfileTile extends StatefulWidget {
final Profile profile;
ProfileTile({this.profile});
@override
_ProfileTileState createState() => _ProfileTileState();
}
class _ProfileTileState extends State<ProfileTile> {
@override
Widget build(BuildContext context) {
return Padding(
padding: EdgeInsets.only(top: 8),
child: Card(
margin: EdgeInsets.fromLTRB(20, 6, 20, 0),
child: ListTile(
leading: CircleAvatar(
radius: 25,
backgroundColor: Colors.green,
),
title: Text(widget.profile.userName),
subtitle: Text(widget.profile.city),
),
),
);
}
}
解决方案
看来,
itemCount:profiles.length,
在提供者向其发送数据之前呈现部分。所以配置文件变量为空。
尝试这个,
itemCount: (profiles == null) ? 0 : profile.length,
推荐阅读
- spring-boot - Spring Boot 2.0.0+ 分页、maven 和 hibernate
- freertos - Cortex M3 - Systick NVIC 禁用 - 节能 FreeRTOS
- bitmap - SDL 如何打包像素?
- aws-lambda - Lambda ARN 的默认 IAM 权限是什么?
- traefik - traefik 错误的 TXT 记录
- css - 如何使我的主图像出现在我的标头和按钮部分的背景中
- angular - 我的服务可以扩展抽象类吗?我的抽象类应该是@Injectable 吗?
- javascript - 传递给 Angular 组件的路由数据未正确设置
- python - 引发“pytesseract.pytesseract.TesseractError:(3221225477,'')”
- angularjs - 绘制死锁图(使用函数)并增加缩放能力