1- So if I have a Bloc that has to be above MaterialApp for theme changing and another bloc that would be just fine if it is above HomePage, should I make two separate providers, one above MaterialApp and the other above HomePage or one MultiBlocProvider at the very top?
2- I am trying to get my download progress to show up in my UI and it does. Problem is how can I tell the bloc the download was completed/failed? I donβt have a context in my repository to call like Bloc.add(DownloadComplete);
This is the script:
typedef DownloadPercentageChanged = void Function(double percentage);
Future downloadFile(
DownloadPercentageChanged onDownloadPercentageChanged,
) {
final request = http.Request(βGETβ, Uri.parse(url);
final response = await httpClient.send(request);
final contentLength = response.contentLength;
final List<int> bytes = [];
response.stream.listen((newBytes) {
bytes.addAll(newBytes);
onDownloadPercentageChanged?.call(bytes.length / contentLength * 100);
},
onDone: //NEED TO INFORM MY BLOC DOWNLOAD WAS SUCCESSFULL,
onError: //NEED TO INFORM MY BLOC DOWNLOAD FAILED
});
}
My Bloc:
if(event is DownloadFileRequested) {
await repository.downloadFile(onDownloadPercentageChanged: (percentage) => add(FileDownloadPercentageChanged(percentage));
} else if(event is FileDownloadPercentageChanged) {
yield const VideoDownloading(event.percentage);
}
Much thanks everyone!
Hi @DeadlyMissile π
1 - You should aim at providing your blocs as lower as possible while being available to all the widgets that care about them. If your second bloc handles stuff in your home page then scope it to that page.
2 - Take the same approach like you did with percentage changed and expose callbacks like onDownloadDone and onDownloadError which will allow you to add proper events to bloc and handle them based on your needs. π
Hi @RollyPeres !
Of course haha that could work! Do you think calling yield VideoDownloading is a bad idea? As far as I know, this willl create a new instance of this class everytime percentage changes, right?
@DeadlyMissile it's fine as long as percentage is the only field on your state class. If your state class is more complex then you'd usually go with yield state.copyWith(percentage: event.percentage);
But considering you're aiming at handling done and error for the download then you'd have to think how to structure your state/s in terms of UI.
copyWith approach I suggested above in order to preserve existing fields while percentage changes.yield const VideoDownloading(event.percentage); makes sense.@RollyPeres Peres one last thing I am so sorry! How do I pass the bloc to my Dialog? What I did was:
In my Dialog class:
final MyBoloc bloc;
BlocConsumer<>(
bloc: widget.bloc,
);
Is this how it is done?
The dialog is shown in a new route so you won't have access to your bloc. You'd have to pass it to your dialog like:
showDialog(
context: context,
builder: (_) => BlocProvider<MyBloc>.value(
value: context.bloc<MyBloc>(),
child: AlertDialog(
title: Text('Title'),
content: Text('Content'),
),
),
);
@RollyPeres I think I am going to cry. Everything is working fine!! Perez I love you more than my wife. Thank you for everything π β€οΈ!
I'm flattered @DeadlyMissile but I would strongly advise you to stick with your wife π
You're welcome and I'm glad things are working well β€
Most helpful comment
I'm flattered @DeadlyMissile but I would strongly advise you to stick with your wife π
You're welcome and I'm glad things are working well β€