0

I want to manage the states of my app with bloc pattern, I have searched on the internet and find a lot of topics, but all are using third party libraries as I don't want.

So is there any resources or example which can help us to use bloc pattern without relying on any external library like: bloc, flutter_bloc, rxdart?

I just want to use the flutter built-in capabilities, not any additional libraries.

Muhammad
  • 2,572
  • 4
  • 24
  • 46

1 Answers1

2

Here's a snippet from my old code, but I highly recommend using bloc and flutter_bloc, it removes a lot of boilerplate code, it's safer and just more known, VSCode plugin handles a lot of code-generation too.

class VotesBloc {
  final List<VoteCount> voteGroups = []; // initial state

  // broadcasting stream so it can be used multiple times
  final _controlStateController = StreamController<List<VoteCount>>.broadcast();

  StreamSink<List<VoteCount>> get _incomingVote => _controlStateController.sink;

  Stream<List<VoteCount>> get outgoingVote => _controlStateController.stream;

  final _votesEventController = StreamController<VoteEvent>.broadcast();

  Sink<VoteEvent> get votesEventSink => _votesEventController.sink;

  VotesBloc() {
    _votesEventController.stream.listen(_mapValuesToState);
  }

  void _mapValuesToState(VoteEvent event) {
    if (event is NewVoteEvent) {
      // handle this state
    } else if (event is VoteChangedEvent) {
      // handle this state
    }

    _incomingVote.add(voteGroups);
  }

  void dispose() {
    _controlStateController.close();
    _votesEventController.close();
  }
}

abstract class VoteEvent {
  final Vote _vote;

  VoteEvent(this._vote);
}

class NewVoteEvent extends VoteEvent {
  NewVoteEvent(Vote vote) : super(vote);
}

class VoteChangedEvent extends VoteEvent {
  VoteChangedEvent(Vote vote) : super(vote);
}

class VoteCount {
  final String value;

  int counter;
  List<Vote> votes;

  VoteCount(this.value, {this.counter, this.votes});
}

And builder function:

StreamBuilder<List<VoteCount>>(
  stream: votesBloc.outgoingVote,
  initialData: votesBloc.voteGroups,
  builder: (BuildContext context, AsyncSnapshot<List<VoteCount>> snapshot) {
    return Widget();
  }
)

Use bloc outside:

votesBloc.votesEventSink.add(VoteChangedEvent(vote));
agilob
  • 6,082
  • 3
  • 33
  • 49
  • Wow thank you great code, could you please share any online docs for these classes you have used? Why I dont want third party library? because the company I am working for them, they never suggest us to use third party packages as they think the third party packages are not maintained for a long time. – Muhammad May 20 '20 at 17:13
  • Is there any difference between `Sink` and `StreamSink` as you have use both of them, is that mondatory – Muhammad May 20 '20 at 17:14
  • One stream is for publishing events, the other for publishing them. Could engineer around without having two, I found having two more aligned with "separation of concerns" principle. – agilob May 20 '20 at 18:46