首先,我看到了这样的错误
I/flutter ( 3245): BLOC Start
I/flutter ( 3245): BLOC Start
════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following NoSuchMethodError was thrown building BlocBuilder<StationBloc, StationState>(dirty, state: _BlocBuilderBaseState<StationBloc, StationState>#24ede):
The getter 'length' was called on null.
Receiver: null
Tried calling: length
The relevant error-causing widget was:
BlocBuilder<StationBloc, StationState> file:///C:/Users/junia/FlutterApp/airportstation/airport_station/lib/view/home_screen.dart:42:18
When the exception was thrown, this was the stack:
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
#1 _HomeScreenState.buildStationList (package:airport_station/view/home_screen.dart:79:30)
#2 _HomeScreenState.build.<anonymous closure> (package:airport_station/view/home_screen.dart:51:24)
#3 BlocBuilder.build (package:flutter_bloc/src/bloc_builder.dart:90:57)
#4 _BlocBuilderBaseState.build (package:flutter_bloc/src/bloc_builder.dart:162:48)
然后,我去了stackoverflow,找到了一个小解决方案
Widget buildStationList(List<AllFlight> allFlight) {
return ListView.builder(
itemCount: allFlight.length,
itemBuilder: (BuildContext context, int index) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: InkWell(
child: ListTile(
title: Text(allFlight[index].airportName + ' - ' + allFlight[index].countryName),
subtitle: Text(allFlight[index].label),
),
),
);
}
);
问题出在itemCount: allFlight.length,
上,我将其更改为itemCount: allFlight?.length ?? 0,
在我再次运行它之后。仍然有错误
I/flutter ( 3245): BLOC Start
I/flutter ( 3245): BLOC Start
E/flutter ( 3245): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: UnimplementedError
E/flutter ( 3245): #0 StationLoadedState.props (package:airport_station/bloc/station_state.dart:23:29)
E/flutter ( 3245): #1 Equatable.== (package:equatable/src/equatable.dart:50:18)
E/flutter ( 3245): #2 Bloc._bindEventsToStates.<anonymous closure> (package:bloc/src/bloc.dart:261:32)
E/flutter ( 3245): #3 _rootRunUnary (dart:async/zone.dart:1198:47)
E/flutter ( 3245): #4 _CustomZone.runUnary (dart:async/zone.dart:1100:19)
E/flutter ( 3245): #5 _CustomZone.runUnaryGuarded (dart:async/zone.dart:1005:7)
E/flutter ( 3245): #6 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:357:11)
E/flutter ( 3245): #7 _BufferingStreamSubscription._add (dart:async/stream_impl.dart:285:7)
E/flutter ( 3245): #8 _SyncBroadcastStreamController._sendData (dart:async/broadcast_stream_controller.dart:385:25)
E/flutter ( 3245): #9 _BroadcastStreamController._add (dart:async/broadcast_stream_controller.dart:293:5)
E/flutter ( 3245): #10 _rootRunUnary (dart:async/zone.dart:1198:47)
E/flutter ( 3245): #11 _CustomZone.runUnary (dart:async/zone.dart:1100:19)
E/flutter ( 3245): #12 _CustomZone.runUnaryGuarded (dart:async/zone.dart:1005:7)
E/flutter ( 3245): #13 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:357:11)
E/flutter ( 3245): #14 _BufferingStreamSubscription._add (dart:async/stream_impl.dart:285:7)
E/flutter ( 3245): #15 _ForwardingStreamSubscription._add (dart:async/stream_pipe.dart:127:11)
E/flutter ( 3245): #16 _MapStream._handleData (dart:async/stream_pipe.dart:224:10)
E/flutter ( 3245): #17 _ForwardingStreamSubscription._handleData (dart:async/stream_pipe.dart:157:13)
E/flutter ( 3245): #18 _rootRunUnary (dart:async/zone.dart:1198:47)
E/flutter ( 3245): #19 _CustomZone.runUnary (dart:async/zone.dart:1100:19)
E/flutter ( 3245): #20 _CustomZone.runUnaryGuarded (dart:async/zone.dart:1005:7)
E/flutter ( 3245): #21 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:357:11)
E/flutter ( 3245): #22 _DelayedData.perform (dart:async/stream_impl.dart:611:14)
E/flutter ( 3245): #23 _StreamImplEvents.handleNext (dart:async/stream_impl.dart:730:11)
E/flutter ( 3245): #24 _PendingEvents.schedule.<anonymous closure> (dart:async/stream_impl.dart:687:7)
E/flutter ( 3245): #25 _rootRun (dart:async/zone.dart:1182:47)
E/flutter ( 3245): #26 _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter ( 3245): #27 _CustomZone.runGuarded (dart:async/zone.dart:997:7)
E/flutter ( 3245): #28 _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1037:23)
E/flutter ( 3245): #29 _rootRun (dart:async/zone.dart:1190:13)
E/flutter ( 3245): #30 _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter ( 3245): #31 _CustomZone.runGuarded (dart:async/zone.dart:997:7)
E/flutter ( 3245): #32 _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1037:23)
E/flutter ( 3245): #33 _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
E/flutter ( 3245): #34 _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
E/flutter ( 3245):
我的bloc_state.dart看起来像这样
...
class StationLoadedState extends StationState {
final List<AllFlight> allFlight;
StationLoadedState({ @required this.allFlight });
@override
List<Object> get props => throw UnimplementedError();
}
...
和BlocBuilder
class _HomeScreenState extends State<HomeScreen> {
StationBloc stationBloc;
@override
void initState() {
super.initState();
stationBloc = BlocProvider.of(context);
stationBloc.add(FetchStationEvent());
}
...
body: Container(
child: BlocListener<StationBloc, StationState>(
listener: (context, state) {
if (state is StationErrorState) {
print(state.message);
}
},
child: BlocBuilder<StationBloc, StationState>(
builder: (context, state) {
print('BLOC Start');
if (state is StationInitialState) {
return buildLoading();
} else if (state is StationLoadingState) {
return buildLoading();
} else if (state is StationLoadedState) {
return buildStationList(state.allFlight);
} else if (state is StationErrorState) {
return buildError(state.message);
}
},
),
),
),
...
当我尝试在http响应上打印时,json响应成功返回。有人知道问题出在哪里吗?
答案 0 :(得分:0)
BlocState有一个“ props”吸气剂,该吸气剂应返回变量数组以相互比较状态。当您连续两次产生相同状态时,它将使用这些道具比较它们,并且如果它与当前状态相同-团体将不会再次产生它(这是使用Equatable时的默认工作方式)。 / p>
在上面的代码中,您有以下内容:
class StationLoadedState extends StationState {
final List<AllFlight> allFlight;
StationLoadedState({ @required this.allFlight });
@override
List<Object> get props => throw UnimplementedError();
}
调用道具时会引发UnimplementedError。将其更改为带有状态参数的数组。
class StationLoadedState extends StationState {
final List<AllFlight> allFlight;
StationLoadedState({ @required this.allFlight });
@override
List<Object> get props => [allFlight];
}