Created
October 18, 2022 12:28
-
-
Save geekswamp/76dc833bc67f63d6b1d4b618953f4ea2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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