Skip to content

Instantly share code, notes, and snippets.

@arifai
Created October 18, 2022 12:28
Show Gist options
  • Save arifai/76dc833bc67f63d6b1d4b618953f4ea2 to your computer and use it in GitHub Desktop.
Save arifai/76dc833bc67f63d6b1d4b618953f4ea2 to your computer and use it in GitHub Desktop.
class Api{
static Future<dynamic> post(String path,
{dynamic body, Map<String, dynamic>? params}) async {
try {
final resp = await dio.post<Map<String, dynamic>>(path,
data: body, queryParameters: params);
return resp.data;
} catch (e) {
ApiResources.errorHandlerFunc(e);
} finally {
dio.close();
}
}
}
class ProfileBloc extends Bloc<ProfileEvent, ProfileState> {
static ProfileState get initialState => ProfileInitialState();
ProfileBloc() : super(initialState) {
on<OnUpdateProfile>((event, emit) async {
emit(UpdateProfileLoading());
try {
final FormData entries = FormData.fromMap({
'image': await MultipartFile.fromFile(event.image),
'address': MultipartFile.fromString(
jsonEncode({"street": "Jl. Jalan", "postalCode": "123456"}),
contentType: MediaType.parse('application/json'),
),
}, ListFormat.multiCompatible);
final resp =
await Api.post('/profile/v1/update', body: entries);
if (resp['code'] == 200) {
emit(ProfileUpdate());
} else {
emit(ProfileUpdateFailure('Cannot send data from server.'));
}
} catch (e) {
emit(ProfileUpdateFailure('$e'));
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment