I tried to migrate flutter_bloc v 8.0.0. After I read the documentation, then I know that I need to migrate mapEventToState to on. After that I don't get an error but _getMonks event is not trigger. Here is my code. Is there anything I miss out?
class MonkBloc extends Bloc<MonkEvent, MonkState> {
final MonkRespository monkRespository;
late List<Monk> monks;
MonkBloc({required this.monkRespository}) : super(MonkInitial()) {
on<GetMonksEvent>((event, emit) => _getMonks);
on<MonkSearchEvent>((event, emit) => _searchMonks);
}
Stream<MonkState> _getMonks(Emitter<MonkState> emit) async* {
emit(MonkLoading());
try {
final List<Monk> monks = await monkRespository.fetchMonks();
emit(MonkLoaded(monks: monks));
} catch (e) {
emit (MonkError(error: (e.toString())));
}
}
Stream<MonkState> _searchMonks(String query, Emitter<MonkState> emit) async* {
emit(MonkLoading());
try {
final List<Monk> monks = await monkRespository.searchMonks(query);
emit(MonkLoaded(monks: monks));
} catch (e) {
emit(MonkError(error: (e.toString())));
}
}
}
class MonkScreen extends StatefulWidget {
static const routeName = '/monk';
final String? title;
final MonkScreenMode? screenMode;
const MonkScreen({Key? key, this.title, this.screenMode}) : super(key: key);
@override
State<MonkScreen> createState() => _MonkScreenState();
}
class _MonkScreenState extends State<MonkScreen> {
_loadMonks() async {
BlocProvider.of<MonkBloc>(context).add(const GetMonksEvent());
}
@override
void initState() {
super.initState();
_loadMonks();
}
@override
Widget build(BuildContext context) {
return BaseWidget(
child: Scaffold(
backgroundColor: Theme.of(context).backgroundColor,
appBar: AppBar(
centerTitle: true,
backgroundColor: Theme.of(context).backgroundColor,
elevation: 0,
title: AutoSizeText(
widget.title!,
style: Theme.of(context).appBarTheme.titleTextStyle,
),
leading: IconButton(
onPressed: () {
Navigator.pop(context);
},
icon: Icon(
Icons.arrow_back,
color: Theme.of(context).primaryIconTheme.color!,
),
),
actions: [
IconButton(
onPressed: () {
showSearch(
context: context,
delegate: MonkSearch(screenMode: widget.screenMode!));
},
icon: Icon(
Icons.search,
color: Theme.of(context).primaryIconTheme.color!,
),
),
],
),
body: BlocBuilder<MonkBloc, MonkState>(
builder: (BuildContext context, MonkState monkState) {
if (monkState is MonkError) {
return const SomethingWentWrongScreen();
} else if (monkState is MonkLoaded) {
return listViewBuilder(
context, monkState.monks, widget.screenMode!);
}
return const Center(
child: CircularProgressIndicator(),
);
},
),
),
);
}
}
CodePudding user response:
This is how I migrate the code. In case someone needed it.
class MonkBloc extends Bloc<MonkEvent, MonkState> {
final MonkRespository monkRespository;
late List<Monk> monks;
MonkBloc({required this.monkRespository}) : super(MonkInitial()) {
on<GetMonksEvent>((event, emit) async {
await _getMonks(emit);
});
on<MonkSearchEvent>((event, emit) async {
await _searchMonks(event.query, emit);
});
}
Future<void> _getMonks(Emitter<MonkState> emit) async {
emit(MonkLoading());
try {
final List<Monk> monks = await monkRespository.fetchMonks();
emit(MonkLoaded(monks: monks));
} catch (e) {
emit (MonkError(error: (e.toString())));
}
}
Future<void> _searchMonks(String query, Emitter<MonkState> emit) async
{
emit(MonkLoading());
try {
final List<Monk> monks = await monkRespository.searchMonks(query);
emit(MonkLoaded(monks: monks));
} catch (e) {
emit(MonkError(error: (e.toString())));
}
}
}
CodePudding user response:
class APIBloc extends Bloc<APIEvents, APIState> {
//
final APIRepo apiRepo;
APIBloc({required this.apiRepo}) : super(APIInitState()) {
on<APIEvents>(_addToValue);
}
Future<void> _addToValue(APIEvents event, Emitter<APIState> emit) async {
print("onEvent");
emit(APILoading());
emit.forEach(
Stream.fromFuture(_callAPI(event).catchError((onError) {
return one rror;
})), onData: (value) {
if (value is Response)
return APILoaded(response: value as Response);
else {
return value as APIListError;
}
});
}
Future<Object> _callAPI(APIEvents event) async {
try {
if (event is FetchDashBoard) {
return await apiRepo.getDashboardData(event.requestParams);
} else if (event is DoLogin) {
return await apiRepo.doLogin(event.requestParams);
} else {
throw (APIListError(
error: NoInternetException('No Internet'),
));
}
} on SocketException {
throw (APIListError(
error: NoInternetException('No Internet'),
));
} on HttpException {
throw (APIListError(
error: NoServiceFoundException('No Service Found'),
));
} on FormatException {
throw (APIListError(
error: InvalidFormatException('Invalid Response format'),
));
} catch (e) {
throw (APIListError(
error: UnknownException('Unknown Error'),
));
}
}
}
follow below site if need more clarify code. https://flutterlearneasyway.blogspot.com/2021/11/flutter-bloc-pattern-800-call-api.html