0

I am using this package: flutter_bloc for my project.

But I have a problem when my home screen has 2 widgets. 1 widget to display news most views of the day, 1 widget displays the news that is suitable for each user object. Each of these cases calls for a different api.

Each widget has two statuses: loading and loading. So what is the best way I can make this case? I don't want to use 2 bloc in 1 screen, I want them to be just 1 home_bloc.

Doan Bui
  • 3,572
  • 25
  • 36

1 Answers1

0

You can combine the two network calls, call them directly

try {
    List responses = await Future.wait([firstNetworkCall(), secondNetworkCall()]);
} catch (e) {
    handleError(e)
}

And use events to fire the network calls

abstract class ExampleBlocEvent{}

ExampleBlocFetchData extends ExampleBlocEvent{
  // use the attributes you need here
}

And use states like this

abstract class ExampleBlocState{}

class ExampleBlocLoading extends ExampleBlocState{}

class ExampleBlocHasData extends ExampleBlocState{
  // Data for first network call
  // Data for second network call
}

That is how you can achieve a single event and single state for the two widgets.

  • How to know which data corresponds to which call to add to ExampleBlocHasData List responses = await Future.wait([firstNetworkCall(), secondNetworkCall()]); – Doan Bui May 12 '20 at 03:44
  • responses[0] will contain the result for the first call and responses[1] will for the second one. – Saiful Islam Adar May 12 '20 at 05:00