Skip to content

Commit 8ad56db

Browse files
committed
update bloc data flow implementation
1 parent bb05c4d commit 8ad56db

File tree

7 files changed

+188
-140
lines changed

7 files changed

+188
-140
lines changed

lib/main.dart

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,51 @@
11
import 'package:flutter/material.dart';
2+
import 'package:flutter_bloc/flutter_bloc.dart';
3+
import 'package:flutter_weather/src/api/api_keys.dart';
4+
import 'package:flutter_weather/src/bloc/weather_bloc_observre.dart';
25
import 'package:flutter_weather/src/screens/routes.dart';
36
import 'package:flutter_weather/src/screens/weather_screen.dart';
47
import 'package:bloc/bloc.dart';
58
import 'package:flutter_weather/src/themes.dart';
69
import 'package:flutter_weather/src/utils/constants.dart';
710
import 'package:flutter_weather/src/utils/converters.dart';
811
import 'package:shared_preferences/shared_preferences.dart';
12+
import 'package:http/http.dart' as http;
913

10-
void main() {
11-
BlocSupervisor().delegate = SimpleBlocDelegate();
12-
runApp(AppStateContainer(child: WeatherApp()));
13-
}
14+
import 'src/api/weather_api_client.dart';
15+
import 'src/bloc/weather_bloc.dart';
16+
import 'src/repository/weather_repository.dart';
1417

15-
class SimpleBlocDelegate extends BlocDelegate {
16-
@override
17-
onTransition(Bloc bloc, Transition transition) {
18-
super.onTransition(bloc, transition);
19-
print(transition);
20-
}
18+
void main() {
19+
Bloc.observer = SimpleBlocObserver();
20+
21+
final WeatherRepository weatherRepository = WeatherRepository(
22+
weatherApiClient: WeatherApiClient(
23+
httpClient: http.Client(),
24+
apiKey: ApiKey.OPEN_WEATHER_MAP,
25+
),
26+
);
27+
28+
runApp(AppStateContainer(
29+
child: WeatherApp(weatherRepository: weatherRepository),
30+
));
2131
}
2232

2333
class WeatherApp extends StatelessWidget {
34+
final WeatherRepository weatherRepository;
35+
36+
WeatherApp({Key key, @required this.weatherRepository})
37+
: assert(weatherRepository != null),
38+
super(key: key);
39+
2440
@override
2541
Widget build(BuildContext context) {
2642
return MaterialApp(
2743
title: 'Flutter Weather App',
2844
theme: AppStateContainer.of(context).theme,
29-
home: WeatherScreen(),
45+
home: BlocProvider(
46+
create: (context) => WeatherBloc(weatherRepository: weatherRepository),
47+
child: WeatherScreen(),
48+
),
3049
routes: Routes.mainRoute,
3150
);
3251
}
@@ -43,9 +62,9 @@ class AppStateContainer extends StatefulWidget {
4362
_AppStateContainerState createState() => _AppStateContainerState();
4463

4564
static _AppStateContainerState of(BuildContext context) {
46-
return (context.inheritFromWidgetOfExactType(_InheritedStateContainer)
47-
as _InheritedStateContainer)
48-
.data;
65+
var widget =
66+
context.dependOnInheritedWidgetOfExactType<_InheritedStateContainer>();
67+
return widget.data;
4968
}
5069
}
5170

@@ -54,7 +73,6 @@ class _AppStateContainerState extends State<AppStateContainer> {
5473
int themeCode = Themes.DARK_THEME_CODE;
5574
TemperatureUnit temperatureUnit = TemperatureUnit.celsius;
5675

57-
5876
@override
5977
initState() {
6078
super.initState();

lib/src/api/weather_api_client.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ class WeatherApiClient {
2020
final url =
2121
'$baseUrl/data/2.5/weather?lat=$latitude&lon=$longitude&appid=$apiKey';
2222
print('fetching $url');
23-
final res = await this.httpClient.get(url);
23+
24+
final res = await this.httpClient.get(Uri.parse(url));
2425
if (res.statusCode != 200) {
2526
throw HTTPException(res.statusCode, "unable to fetch weather data");
2627
}
@@ -31,7 +32,8 @@ class WeatherApiClient {
3132
Future<Weather> getWeatherData(String cityName) async {
3233
final url = '$baseUrl/data/2.5/weather?q=$cityName&appid=$apiKey';
3334
print('fetching $url');
34-
final res = await this.httpClient.get(url);
35+
final res = await this.httpClient.get(Uri.parse(url));
36+
3537
if (res.statusCode != 200) {
3638
throw HTTPException(res.statusCode, "unable to fetch weather data");
3739
}
@@ -42,7 +44,7 @@ class WeatherApiClient {
4244
Future<List<Weather>> getForecast(String cityName) async {
4345
final url = '$baseUrl/data/2.5/forecast?q=$cityName&appid=$apiKey';
4446
print('fetching $url');
45-
final res = await this.httpClient.get(url);
47+
final res = await this.httpClient.get(Uri.parse(url));
4648
if (res.statusCode != 200) {
4749
throw HTTPException(res.statusCode, "unable to fetch weather data");
4850
}

lib/src/bloc/weather_bloc.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ import 'package:flutter_weather/src/bloc/weather_state.dart';
77
import 'package:flutter_weather/src/repository/weather_repository.dart';
88
import 'package:flutter_weather/src/api/http_exception.dart';
99

10+
import 'weather_state.dart';
11+
1012
class WeatherBloc extends Bloc<WeatherEvent, WeatherState> {
1113
final WeatherRepository weatherRepository;
1214

1315
WeatherBloc({@required this.weatherRepository})
14-
: assert(weatherRepository != null);
16+
: assert(weatherRepository != null),
17+
super(WeatherEmpty());
1518

1619
@override
1720
WeatherState get initialState {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import 'package:bloc/bloc.dart';
2+
3+
class SimpleBlocObserver extends BlocObserver {
4+
@override
5+
void onEvent(Bloc bloc, Object event) {
6+
super.onEvent(bloc, event);
7+
print('onEvent $event');
8+
}
9+
10+
@override
11+
onTransition(Bloc bloc, Transition transition) {
12+
super.onTransition(bloc, transition);
13+
print('onTransition $transition');
14+
}
15+
16+
@override
17+
void onError(BlocBase bloc, Object error, StackTrace stackTrace) {
18+
print('onError $error');
19+
super.onError(bloc, error, stackTrace);
20+
}
21+
}

lib/src/bloc/weather_event.dart

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import 'package:equatable/equatable.dart';
22

33
abstract class WeatherEvent extends Equatable {
4-
WeatherEvent([List props = const []]) : super(props);
4+
WeatherEvent();
55
}
66

77
class FetchWeather extends WeatherEvent {
@@ -10,7 +10,8 @@ class FetchWeather extends WeatherEvent {
1010
final double latitude;
1111

1212
FetchWeather({this.cityName, this.longitude, this.latitude})
13-
: assert(cityName != null || longitude != null || latitude != null),
14-
super([cityName, longitude, latitude]);
15-
}
13+
: assert(cityName != null || longitude != null || latitude != null);
1614

15+
@override
16+
List<Object> get props => [cityName, longitude, latitude];
17+
}

lib/src/bloc/weather_state.dart

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import 'package:meta/meta.dart';
33
import 'package:equatable/equatable.dart';
44

55
abstract class WeatherState extends Equatable {
6-
WeatherState([List props = const []]) : super(props);
6+
WeatherState();
7+
8+
@override
9+
List<Object> get props => [];
710
}
811

912
class WeatherEmpty extends WeatherState {}
@@ -13,15 +16,17 @@ class WeatherLoading extends WeatherState {}
1316
class WeatherLoaded extends WeatherState {
1417
final Weather weather;
1518

16-
WeatherLoaded({@required this.weather})
17-
: assert(weather != null),
18-
super([weather]);
19+
WeatherLoaded({@required this.weather}) : assert(weather != null);
20+
21+
@override
22+
List<Object> get props => [weather];
1923
}
2024

2125
class WeatherError extends WeatherState {
2226
final int errorCode;
2327

24-
WeatherError({@required this.errorCode})
25-
: assert(errorCode != null),
26-
super([errorCode]);
28+
WeatherError({@required this.errorCode}) : assert(errorCode != null);
29+
30+
@override
31+
List<Object> get props => [errorCode];
2732
}

0 commit comments

Comments
 (0)