Skip to content

Commit

Permalink
🆒 Make bloc controllers private
Browse files Browse the repository at this point in the history
  • Loading branch information
toureholder committed Jun 14, 2019
1 parent 4413db7 commit 0c0b89f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
10 changes: 5 additions & 5 deletions lib/feature/home/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ class Home extends StatefulWidget {
}

class _HomeState extends State<Home> {
HomeBloc bloc;
HomeBloc _bloc;

@override
void initState() {
super.initState();
bloc = HomeBloc();
bloc.loadDonations();
_bloc = HomeBloc();
_bloc.loadDonations();
}

@override
void dispose() {
bloc.dispose();
_bloc.dispose();
super.dispose();
}

Expand All @@ -41,7 +41,7 @@ class _HomeState extends State<Home> {

StreamBuilder<List<Donation>> _listFutureBuilder() {
return StreamBuilder(
stream: bloc.controller.stream,
stream: _bloc.stream,
builder:
(BuildContext context, AsyncSnapshot<List<Donation>> snapshot) {
Widget widget = Center(child: CircularProgressIndicator());
Expand Down
10 changes: 6 additions & 4 deletions lib/feature/home/home_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@ import 'package:flutter_workshop/model/donation/donation.dart';
import 'package:flutter_workshop/model/donation/donation_api.dart';

class HomeBloc {
final StreamController controller = StreamController<List<Donation>>();
final StreamController _controller = StreamController<List<Donation>>();

get stream => _controller.stream;

dispose() {
controller.close();
_controller.close();
}

loadDonations() async {
try {
final donations = await DonationApi().getDonations();
controller.sink.add(donations);
_controller.sink.add(donations);
} catch (error) {
controller.sink.addError(error);
_controller.sink.addError(error);
}
}
}
2 changes: 1 addition & 1 deletion lib/feature/login/login.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class _LoginState extends State<Login> {
_passwordField(context),
SizedBox(height: 60),
StreamBuilder<HttpEvent<LoginResponse>>(
stream: _bloc.controller.stream,
stream: _bloc.stream,
builder:
(context, AsyncSnapshot<HttpEvent<LoginResponse>> snapshot) {
bool isLoading = snapshot.hasData && snapshot.data.isLoading;
Expand Down
12 changes: 7 additions & 5 deletions lib/feature/login/login_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@ import 'package:flutter_workshop/model/login/login_response.dart';
import 'package:flutter_workshop/util/http_event.dart';

class LoginBloc {
final StreamController controller =
final StreamController _controller =
StreamController<HttpEvent<LoginResponse>>();

dispose() => controller.close();
get stream => _controller.stream;

dispose() => _controller.close();

login(LoginRequest request) async {
try {
controller.sink.add(HttpEvent<LoginResponse>(state: EventState.loading));
_controller.sink.add(HttpEvent<LoginResponse>(state: EventState.loading));
final loginResponse = await LoginApi().login(request);
controller.sink.add(HttpEvent<LoginResponse>(
_controller.sink.add(HttpEvent<LoginResponse>(
state: EventState.done, data: loginResponse));
} catch (error) {
controller.sink.addError(error);
_controller.sink.addError(error);
}
}
}

0 comments on commit 0c0b89f

Please sign in to comment.