我使用Firebase作为后台开发Flutter应用程序,当我从屏幕移动到另一个BottomNavigationBar时,我使用StreamProvider将Profile数据传递到另一个屏幕。
当我移动到另一个屏幕时,我收到这个错误。
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]);
});
}
}
型材瓦
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,
所以profile变量是空的。
试试这个。
itemCount: (profiles == null) ? 0 : profile.length,