简体   繁体   中英

Pass multiple parameters to Sink Dart

I'm studying a BLoC pattern in Flutter with Dart.

Now I have a list that show a Users that have a property 'Age'.

Through 2 buttons on the list I should to be able to increase or decrease Age of every user.

So I have created many stream and Sink like:

/** ADD AGE*/
  BehaviorSubject<User> _userAddAgeController = new BehaviorSubject<User>();
  Sink<User> get inAddAgeUser => _userAddAgeController.sink;

/** REMOVE AGE */
  BehaviorSubject<User> _userDecreaseAgeController = new BehaviorSubject<User>();
  Sink<User> get inDecreaseAgeUser => _userDecreaseAgeController.sink;

and then in the constructor of BlocClass

UserBloc(this.db) {
    _userAddAgeController.listen(_addAge);
    _userDecreaseAgeController.listen(_decreaseAge);
}

And then two function:

void _addAge(User u){ }
void _decreaseAge(User u){ .. }

So My question is:

Is it possibile centralize the controller passing for example 2 parameters for know if I should add or remove?

The only way to pass multiple argument to a Sink is to create a specific object.

class UserAgeEvent {
   final User user;
   final int increaseAge;

   UserAgeEvent(this.user, this.increaseAge);
}

ageUserSink.add(UserAgeEvent(user, 1));
ageUserSink.add(UserAgeEvent(user, -1));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM