我正在应用程序中工作,不使用集团作为状态管理,我想添加更多的功能,(该功能将使用集团),当我尝试运行应用程序时,我得到了这个错误:
=外衣库捕获的异常╞═══════════════════════════════════════════════════════════I/颤振(17157):下面的TransportLoading对象被抛出构建TransportPage(dirty,状态:I/flutter (17157)):_TransportPageState#daf04): I/flutter (17157): Instance of 'TransportLoading'
TransportLoading是block中的一个状态。
这是TransportBloc
class TransportBloc extends Bloc<TransportEvent, TransportState> {
final StudentCircuitRepository studentCircuitRepository ;
TransportBloc({this.studentCircuitRepository}) ;
@override
TransportState get initialState => throw TransportLoading();
@override
Stream<TransportState> mapEventToState (
TransportEvent event,
) async* {
// TODO: implement mapEventToState
List<StudentCircuitModel> student ;
try{
if(event is LoadingStudentCircuit){
student = await studentCircuitRepository.fetchStudentCircuit(event.studentList);
}
if(student.length == 0){
yield TransportEmpty();
}else{
yield TransportLoaded(studentCircuit: student);
}
}catch (_) {
yield TransportError();
}
}
}
这是我尝试使用TransportBloc的页面
class TransportPageBloc extends StatefulWidget {
final List<StudentUser> stdUser;
TransportPageBloc(this.stdUser);
@override
_TransportBloxState createState() => _TransportBloxState();
}
class _TransportBloxState extends State<TransportPageBloc> {
@override
Widget build(BuildContext context) {
return BlocProvider<TransportBloc>(
create: (BuildContext context) => TransportBloc(),
child: TransportPage(widget.stdUser),
);
}
}
class TransportPage extends StatefulWidget {
final List<StudentUser> student;
TransportPage(this.student);
@override
_TransportPageState createState() => _TransportPageState();
}
class _TransportPageState extends State<TransportPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Liste des étudiants'),
),
body:
BlocBuilder<TransportBloc, TransportState>(bloc: TransportBloc(),builder: (context, state) {
if (state is TransportLoading) {
BlocProvider.of<TransportBloc>(context).add(LoadingStudentCircuit(studentList: widget.student));
return _initialWidget();
} else if (state is TransportLoaded) {
return _listStudentCircuit(state.studentCircuit);
} else if (state is TransportEmpty) {
return _emptyTransport();
} else if (state is TransportError) {
return _errorWidget();
}
return null;
}),
);
}
}
Widget _initialWidget() {
return Center(
child: CircularProgressIndicator(),
);
}
Widget _listStudentCircuit(List<StudentCircuitModel> studentCircuit) {
return ListView.separated(
itemBuilder: (context, index) {
return Container(
child: Text(studentCircuit[index].eleve.name),
);
},
separatorBuilder: (context, index) {
return Divider(
height: 5.0,
);
},
itemCount: studentCircuit.length);
}
Widget _emptyTransport(){
return Center(
child: Text('Verifier que vous avez des enfant'),
);
}
Widget _errorWidget(){
return Center(
child: Text('this is the error page'),
);
}
您正在使用throw
来设置初始状态,而这不是throw
的作用。
替换这个
@override
TransportState get initialState => throw TransportLoading();
在你的TransportBloc
TransportPageBloc() : super(TransportLoading());