Bloc: A few questions

Created on 13 Jun 2020  Β·  7Comments  Β·  Source: felangel/bloc

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!

question

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 ❀

All 7 comments

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.

  • if percentage, done and error message could simultaneously be present on your UI then have a single state class with these fields and use the copyWith approach I suggested above in order to preserve existing fields while percentage changes.
  • if percentage, done and error message will be present in your UI one at a time then go with different state classes and yield a new instance of the proper class. In this case 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 ❀

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fvisticot picture fvisticot  Β·  31Comments

zs-dima picture zs-dima  Β·  34Comments

konstantin-doncov picture konstantin-doncov  Β·  30Comments

felangel picture felangel  Β·  32Comments

sawankumarbundelkhandi picture sawankumarbundelkhandi  Β·  108Comments