Describe the bug
A clear and concise description of what the bug is.
Used Package - Dio, Bloc, Flutter_Bloc
Issue - To Get current upload and download status/value
I'm using DIo package and need to get current upload and download status/value during download the file from server or upload single/multiple file (Multipart) to server. Can you tell me how to work with your package and get it because it's urgent and publish the production app of my organization. I need your help, if it's possible then please publish/give us to one example regarding it.
Additional context
Add any other context about the problem here.
Hi @visual-velocity ๐
You can use dio.download(onReceiveProgress: ...) to achieve this.
In your http client layer have a request around the lines of:
typedef DownloadPercentageChanged = void Function(double percentage);
Future downloadFile(
DownloadPercentageChanged onDownloadPercentageChanged,
) {
return _httpClient.download('download path', 'save path',
onReceiveProgress: (received, total) {
onDownloadPercentageChanged?.call((received / total) * 100);
});
}
You'd expose this call through a repository and consume it in your bloc where you'd end up doing something like:
if (event is DownloadFileRequested) {
await _repository.downloadFile(onDownloadPercentageChanged: (percentage) => add(FileDownloadPercentageChanged(percentage)));
...
}
if (event is FileDownloadPercentageChanged) {
yield state.copyWith(downloadPercentageCompleted: event.percentage);
...
}
Hope this gives you a starting point ๐
Can you suggest me how to handle this code?. Please give us suggestion or sample
if (event is FileDownloadPercentageChanged) {
yield state.copyWith(downloadPercentageCompleted: event.percentage); //don't know how to handle it
...
}
That code goes into mapEventToState. downloadPercentageCompleted is a field on the bloc state that you'll be using to build some widget to show the progress on the UI or whatever your requirements might be.
I'm writing this code, can you suggest to us,i am handling this code. Is it
correct or not correct?, because i never used copyWith before your response.
if(event is FileUploadPercentageChangedEvent){
*FileUploadPercentageChangedState *currentState=state;
yield currentState.copyWith(percentage: event.percentage);
}
class FileUploadPercentageChangedEvent *extends *SampleEvent{
final *double *percentage;
FileUploadPercentageChangedEvent({this.percentage});
*FileUploadPercentageChangedEvent *copyWith({
*double *percentage,
}) {
return new FileUploadPercentageChangedEvent(
percentage: percentage ?? this.percentage,
);
}
@override
// TODO: implement props
List<Object> get props => [percentage];
}
class FileUploadPercentageChangedState *extends *SampleState {
final *double *percentage;
FileUploadPercentageChangedState ({this.percentage});
FileUploadPercentageChangedState copyWith({
*double *percentage,
}) {
return new FileUploadPercentageChangedState (
percentage: percentage ?? this.percentage,
);
}
@override
// TODO: implement props
List<Object> get props => [percentage];
}
On Thu, May 28, 2020 at 8:28 PM Rolly Peres notifications@github.com
wrote:
That code goes into mapEventToState. downloadPercentageCompleted is a
field on the bloc state that you'll be using to build some widget to show
the progress on the UI or whatever your requirements might be.โ
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/felangel/bloc/issues/1218#issuecomment-635404040, or
unsubscribe
https://github.com/notifications/unsubscribe-auth/APGW34WZUE4GMQ4PNHEKD6TRTZ32JANCNFSM4NL45ECQ
.
currentState right at the beginning of the mapEventToState.copyWith on event classes.@RollyPeres .Thank you so much for suggestion and feedback.
I have already extends equatable inside event/state class .like
@immutable
abstract class SampleState extends Equatable {
const SampleState();
@override
List
@immutable
abstract class SampleEvent extends Equatable {
const SampleEvent();
@override
List
Can you help me by telling me how can we reuse a previous state.
copyWith is used for that, for example yield currentState.copyWith(percentage: event.percentage);
As always, thanks for taking the time to answer @RollyPeres ๐
@visual-velocity closing this for now but feel free to comment with additional questions and I'm happy to continue the conversation ๐
Let me check and add the code inside production app then i inform to you(@felangel and @RollyPeres )
Hi @felangel and @RollyPeres
class UploadProfileImageEvent extends SampleEvent{
final File filePath;
final String fileName;
UploadProfileImageEvent({this.filePath, this.fileName});
@override
// TODO: implement props
List
class ProfileImageFileUploadPercentageChangedEvent extends SampleEvent{
final double percentage;
ProfileImageFileUploadPercentageChangedEvent({this.percentage});
@override
// TODO: implement props
List
class UploadProfileImageState extends SampleState{
final File filePath;
final String fileName;
UploadProfileImageState({this.filePath, this.fileName});
@override
// TODO: implement props
List
class ProfileImageFileUploadPercentageChangedState extends SampleState{
final double percentage;
ProfileImageFileUploadPercentageChangedState({this.percentage});
@override
// TODO: implement props
List
Repo call
Future
NETWORK_STATUS network_status = await networkConnectivityStatus();
if (network_status == NETWORK_STATUS.CONNECTED) {
try {
FormData formData = FormData.fromMap({
"file": await MultipartFile.fromFile("$filePath",filename: "$fileName"),
});
response = await client.post("${endPoint}", //<-- client is Dio
data: await formData,
onSendProgress: (received,total){
if (total != -1) {
onUploadProgress?.call((received / total) * 100);
}
},
);
if (response.statusCode == SUCCESS) {
return Right(ModelName.fromJson(response.data));
}
} on DioError catch (e) {
return Left(e);
} catch (e) { return Left(e);
}
return await Future.delayed(Duration(microseconds: 1));
}
Bloc Code
if(event is UploadProfileImageEvent){
Either
ProfileImageFileUploadPercentageChangedEvent(percentage: result)
));
yield* res.fold(
(failure) async* {
yield ImageUploadFailedState(failedReason: "Image upload failed");
},
(payload) async* {
yield ImageUploadSuccessState(successMessage: "The profile image has been uploaded successfully.");
},
);
}
if(event is ProfileImageFileUploadPercentageChangedEvent){
if(currentState is ProfileImageFileUploadPercentageChangedState){
yield currentState.copyWith(percentage: event.percentage);
}else{
yield ProfileImageFileUploadPercentageChangedState(percentage: event.percentage);
}
//yield ProfileImageFileUploadPercentageChangedState(percentage: event.percentage);
}
In the Above code, I am getting upload status(ProfileImageFileUploadPercentageChangedState) only once and I am not getting either the image upload success status (ImageUploadSuccessState) or the image upload failed status (ImageUploadFailedState), @felangel and @RollyPeres can you suggest us where am i wrong,please rectify our code and help me, because it's urgent.
@visual-velocity please share a full sample repo that I can run locally. Thanks ๐
Sorry @RollyPeres I'm not able to share full code due to my company protocol. I can share only sample. If you find any trouble tell me how can i help you
Can you please create a small sample app that reproduces your issue ? It would be easier to help you if I see your complete approach. Thanks ๐
As per last statement i understand what are you telling but due to not enough time i can't create sample app. I have shared to you my approach where i shared my event, state, bloc and repo everything. My agenda is i want to upload user profile image but I can't share the company endpoint due to protocol. Uploading process is working fine but i'm not able to get either the image upload success status (ImageUploadSuccessState) or the image upload failed status (ImageUploadFailedState), from repo. Which has already mentioned
I can't really tell what's wrong in your code as long as I can't run it locally; that is precisely why I asked you to create a mock sample app.
You could have a problem when posting your data or the response status might not be success but also not an error. Exclude these scenarios and if you're still having issues consider joining bloc's discord channel and shoot me a private message and I'm willing to set up a meeting to have a look at your implementation. ๐
Sure.
Most helpful comment
Hi @visual-velocity ๐
You can use
dio.download(onReceiveProgress: ...)to achieve this.In your http client layer have a request around the lines of:
You'd expose this call through a repository and consume it in your bloc where you'd end up doing something like:
Hope this gives you a starting point ๐