我正在尝试将 BLoC 模式与 RxDart 一起使用,并且我想通过 StreamBuilder 从列表中获取一个事件。
我的问题是,当我打开我的详细信息屏幕时,像 1 秒我收到以下错误消息:The method 'firstWhere' was called on null.
有没有办法在不添加加载进度的情况下等待获取数据,因为我想将 HeroAnimation 保留在我的图像上?
class _EventsDetailsScreenState extends State<EventsDetailsScreen> {
@override
Widget build(BuildContext context) {
final eventBloc = BlocProvider.of<EventsBloc>(context);
//Event event = eventBloc.events.firstWhere((elmt) => elmt.id == widget.id, orElse:() => null);
return StreamBuilder(
stream : eventBloc.allEvents,
builder: (context, snapshot){
final Event event = snapshot.data.firstWhere((elmt) => elmt.id == widget.id, orElse:() => null);
return Scaffold(
body: Stack(
children: <Widget>[
Column(children: <Widget>[
Expanded(
child: ListView(
shrinkWrap: true,
children: <Widget>[
_buildEventImage(context, event),
(event.description != null && event.description.trim().isNotEmpty) ? _buildDescriptionSection(context, event) : Container(),
],
))
]),
new Positioned(
//Place it at the top, and not use the entire screen
top: 0.0,
left: 0.0,
right: 0.0,
child: AppBar(
actions: <Widget>[
IconButton(icon: Icon(Icons.create), onPressed: () async => print('Edit')),
],
backgroundColor: Colors.transparent, //No more green
elevation: 0.0, //Shadow gone
),
),
],
),
);
},
);
}
Widget _buildEventImage(BuildContext context, Event event) {
return Hero(
tag: 'eventImg${event.id}',
child: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("images/croatia.jpg"),
alignment: Alignment.topCenter,
fit: BoxFit.fill),
),
padding: EdgeInsets.only(left: 40.0, bottom: 250.0),
alignment: Alignment.bottomLeft,
height: MediaQuery.of(context).size.height - 30.0,
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
child: Material(
color: Colors.transparent,
child: Text(
event.name,
style: eventNameTextStyle,
)),
),
Container(
child: Material(
color: Colors.transparent,
child: Text(
"In ${event.date.difference(DateTime.now()).inDays} days",
style: eventDateTextStyle)))
],
)),
);
}