Showing
12 changed files
with
42 additions
and
25 deletions
1 | import 'package:flutter/material.dart'; | 1 | import 'package:flutter/material.dart'; |
2 | import 'package:get/get.dart'; | 2 | import 'package:get/get.dart'; |
3 | -import 'package:get_demo/routes/app_pages.dart'; | 3 | + |
4 | +import 'routes/app_pages.dart'; | ||
4 | import 'shared/logger/logger_utils.dart'; | 5 | import 'shared/logger/logger_utils.dart'; |
5 | 6 | ||
6 | void main() { | 7 | void main() { |
1 | import 'package:dio/dio.dart'; | 1 | import 'package:dio/dio.dart'; |
2 | import 'package:get/get.dart'; | 2 | import 'package:get/get.dart'; |
3 | -import 'package:get_demo/pages/home/domain/adapters/repository_adapter.dart'; | ||
4 | -import 'package:get_demo/pages/home/presentation/controllers/home_controller.dart'; | 3 | + |
5 | import '../data/home_repository.dart'; | 4 | import '../data/home_repository.dart'; |
5 | +import '../domain/adapters/repository_adapter.dart'; | ||
6 | +import '../presentation/controllers/home_controller.dart'; | ||
6 | 7 | ||
7 | class HomeBinding extends Bindings { | 8 | class HomeBinding extends Bindings { |
8 | @override | 9 | @override |
1 | import 'package:dio/dio.dart'; | 1 | import 'package:dio/dio.dart'; |
2 | -import 'package:get_demo/pages/home/domain/adapters/repository_adapter.dart'; | ||
3 | -import 'package:get_demo/pages/home/domain/entity/cases_model.dart'; | 2 | + |
3 | +import '../domain/adapters/repository_adapter.dart'; | ||
4 | +import '../domain/entity/cases_model.dart'; | ||
4 | 5 | ||
5 | class HomeRepository implements IHomeRepository { | 6 | class HomeRepository implements IHomeRepository { |
6 | HomeRepository({this.dio}); | 7 | HomeRepository({this.dio}); |
@@ -13,7 +14,7 @@ class HomeRepository implements IHomeRepository { | @@ -13,7 +14,7 @@ class HomeRepository implements IHomeRepository { | ||
13 | final response = await dio.get("https://api.covid19api.com/summary"); | 14 | final response = await dio.get("https://api.covid19api.com/summary"); |
14 | 15 | ||
15 | return CasesModel.fromJson(response.data as Map<String, dynamic>); | 16 | return CasesModel.fromJson(response.data as Map<String, dynamic>); |
16 | - } catch (e) { | 17 | + } on Exception catch (e) { |
17 | print(e.toString()); | 18 | print(e.toString()); |
18 | return Future.error(e.toString()); | 19 | return Future.error(e.toString()); |
19 | } | 20 | } |
1 | import 'package:get/get.dart'; | 1 | import 'package:get/get.dart'; |
2 | -import 'package:get_demo/pages/home/domain/adapters/repository_adapter.dart'; | ||
3 | -import 'package:get_demo/pages/home/domain/entity/cases_model.dart'; | 2 | + |
3 | +import '../../domain/adapters/repository_adapter.dart'; | ||
4 | +import '../../domain/entity/cases_model.dart'; | ||
4 | 5 | ||
5 | enum Status { loading, success, error } | 6 | enum Status { loading, success, error } |
6 | 7 | ||
@@ -22,14 +23,16 @@ class HomeController extends GetxController { | @@ -22,14 +23,16 @@ class HomeController extends GetxController { | ||
22 | 23 | ||
23 | /// fetch cases from Api | 24 | /// fetch cases from Api |
24 | Future<void> fetchDataFromApi() async { | 25 | Future<void> fetchDataFromApi() async { |
25 | - /// When the repository returns the value, change the status to success, and fill in "cases" | 26 | + /// When the repository returns the value, change the status to success, |
27 | + /// and fill in "cases" | ||
26 | return homeRepository.getCases().then( | 28 | return homeRepository.getCases().then( |
27 | (data) { | 29 | (data) { |
28 | cases(data); | 30 | cases(data); |
29 | status(Status.success); | 31 | status(Status.success); |
30 | }, | 32 | }, |
31 | 33 | ||
32 | - /// In case of error, print the error and change the status to Status.error | 34 | + /// In case of error, print the error and change the status |
35 | + /// to Status.error | ||
33 | onError: (err) { | 36 | onError: (err) { |
34 | print("$err"); | 37 | print("$err"); |
35 | return status(Status.error); | 38 | return status(Status.error); |
1 | import 'dart:ui'; | 1 | import 'dart:ui'; |
2 | + | ||
2 | import 'package:flutter/material.dart'; | 3 | import 'package:flutter/material.dart'; |
3 | import 'package:get/get.dart'; | 4 | import 'package:get/get.dart'; |
4 | -import 'package:get_demo/pages/home/domain/entity/cases_model.dart'; | 5 | + |
5 | import '../controllers/home_controller.dart'; | 6 | import '../controllers/home_controller.dart'; |
6 | 7 | ||
7 | class CountryView extends GetWidget<HomeController> { | 8 | class CountryView extends GetWidget<HomeController> { |
@@ -30,7 +31,7 @@ class CountryView extends GetWidget<HomeController> { | @@ -30,7 +31,7 @@ class CountryView extends GetWidget<HomeController> { | ||
30 | child: ListView.builder( | 31 | child: ListView.builder( |
31 | itemCount: controller.cases.value.countries.length, | 32 | itemCount: controller.cases.value.countries.length, |
32 | itemBuilder: (context, index) { | 33 | itemBuilder: (context, index) { |
33 | - Country country = controller.cases.value.countries[index]; | 34 | + final country = controller.cases.value.countries[index]; |
34 | return ListTile( | 35 | return ListTile( |
35 | onTap: () { | 36 | onTap: () { |
36 | Get.toNamed('/details', arguments: country); | 37 | Get.toNamed('/details', arguments: country); |
@@ -2,12 +2,13 @@ import 'dart:ui'; | @@ -2,12 +2,13 @@ import 'dart:ui'; | ||
2 | 2 | ||
3 | import 'package:flutter/material.dart'; | 3 | import 'package:flutter/material.dart'; |
4 | import 'package:get/get.dart'; | 4 | import 'package:get/get.dart'; |
5 | -import 'package:get_demo/pages/home/domain/entity/cases_model.dart'; | 5 | + |
6 | +import '../../domain/entity/cases_model.dart'; | ||
6 | 7 | ||
7 | class DetailsView extends StatelessWidget { | 8 | class DetailsView extends StatelessWidget { |
8 | @override | 9 | @override |
9 | Widget build(BuildContext context) { | 10 | Widget build(BuildContext context) { |
10 | - Country country = Get.arguments as Country; | 11 | + final country = Get.arguments as Country; |
11 | return Container( | 12 | return Container( |
12 | decoration: BoxDecoration( | 13 | decoration: BoxDecoration( |
13 | image: DecorationImage( | 14 | image: DecorationImage( |
@@ -2,7 +2,8 @@ import 'dart:ui'; | @@ -2,7 +2,8 @@ import 'dart:ui'; | ||
2 | 2 | ||
3 | import 'package:flutter/material.dart'; | 3 | import 'package:flutter/material.dart'; |
4 | import 'package:get/get.dart'; | 4 | import 'package:get/get.dart'; |
5 | -import 'package:get_demo/pages/home/presentation/controllers/home_controller.dart'; | 5 | + |
6 | +import '../controllers/home_controller.dart'; | ||
6 | 7 | ||
7 | class HomeView extends GetView<HomeController> { | 8 | class HomeView extends GetView<HomeController> { |
8 | @override | 9 | @override |
@@ -28,7 +29,7 @@ class HomeView extends GetView<HomeController> { | @@ -28,7 +29,7 @@ class HomeView extends GetView<HomeController> { | ||
28 | body: Center( | 29 | body: Center( |
29 | child: Obx( | 30 | child: Obx( |
30 | () { | 31 | () { |
31 | - Status status = controller.status.value; | 32 | + final status = controller.status.value; |
32 | if (status == Status.loading) return CircularProgressIndicator(); | 33 | if (status == Status.loading) return CircularProgressIndicator(); |
33 | if (status == Status.error) return Text('Error on connection :('); | 34 | if (status == Status.error) return Text('Error on connection :('); |
34 | return Column( | 35 | return Column( |
1 | import 'package:get/get.dart'; | 1 | import 'package:get/get.dart'; |
2 | -import 'package:get_demo/pages/home/bindings/home_binding.dart'; | ||
3 | -import 'package:get_demo/pages/home/presentation/views/country_view.dart'; | ||
4 | -import 'package:get_demo/pages/home/presentation/views/details_view.dart'; | ||
5 | -import 'package:get_demo/pages/home/presentation/views/home_view.dart'; | 2 | + |
3 | +import '../pages/home/bindings/home_binding.dart'; | ||
4 | +import '../pages/home/presentation/views/country_view.dart'; | ||
5 | +import '../pages/home/presentation/views/details_view.dart'; | ||
6 | +import '../pages/home/presentation/views/home_view.dart'; | ||
6 | 7 | ||
7 | part 'app_routes.dart'; | 8 | part 'app_routes.dart'; |
8 | 9 | ||
10 | +// ignore: avoid_classes_with_only_static_members | ||
9 | class AppPages { | 11 | class AppPages { |
10 | static const INITIAL = Routes.HOME; | 12 | static const INITIAL = Routes.HOME; |
11 | 13 |
1 | class Logger { | 1 | class Logger { |
2 | // Sample of abstract logging function | 2 | // Sample of abstract logging function |
3 | static void write(String text, {bool isError = false}) { | 3 | static void write(String text, {bool isError = false}) { |
4 | - print('** ' + text + ' [' + isError.toString() + ']'); | 4 | + print('** $text [$isError]'); |
5 | } | 5 | } |
6 | } | 6 | } |
@@ -49,7 +49,7 @@ void main() { | @@ -49,7 +49,7 @@ void main() { | ||
49 | binding.builder(); | 49 | binding.builder(); |
50 | 50 | ||
51 | /// recover your controller | 51 | /// recover your controller |
52 | - HomeController controller = Get.find(); | 52 | + final controller = Get.find<HomeController>(); |
53 | 53 | ||
54 | /// check if onInit was called | 54 | /// check if onInit was called |
55 | expect(controller.initialized, true); | 55 | expect(controller.initialized, true); |
@@ -4,7 +4,8 @@ import 'dart:collection'; | @@ -4,7 +4,8 @@ import 'dart:collection'; | ||
4 | import '../rx_core/rx_interface.dart'; | 4 | import '../rx_core/rx_interface.dart'; |
5 | 5 | ||
6 | 6 | ||
7 | -/// global object that registers against `GetX` and `Obx`, and allows the reactivity | 7 | +/// global object that registers against `GetX` and `Obx`, and allows the |
8 | +/// reactivity | ||
8 | /// of those `Widgets` and Rx values. | 9 | /// of those `Widgets` and Rx values. |
9 | RxInterface getObs; | 10 | RxInterface getObs; |
10 | 11 | ||
@@ -207,6 +208,9 @@ class RxBool extends _RxImpl<bool> { | @@ -207,6 +208,9 @@ class RxBool extends _RxImpl<bool> { | ||
207 | 208 | ||
208 | /// Toggles the bool [value] between false and true. | 209 | /// Toggles the bool [value] between false and true. |
209 | /// A shortcut for `flag.value = !flag.value;` | 210 | /// A shortcut for `flag.value = !flag.value;` |
211 | + /// FIXME: why return this? fluent interface is not | ||
212 | + /// not really a dart thing since we have '..' operator | ||
213 | + // ignore: avoid_returning_this | ||
210 | RxBool toggle() { | 214 | RxBool toggle() { |
211 | subject.add(_value = !_value); | 215 | subject.add(_value = !_value); |
212 | return this; | 216 | return this; |
@@ -295,7 +299,8 @@ class RxInt extends _BaseRxNum<int> { | @@ -295,7 +299,8 @@ class RxInt extends _BaseRxNum<int> { | ||
295 | } | 299 | } |
296 | 300 | ||
297 | 301 | ||
298 | -/// Foundation class used for custom `Types` outside the common native Dart types. | 302 | +/// Foundation class used for custom `Types` outside the common native Dart |
303 | +/// types. | ||
299 | /// For example, any custom "Model" class, like User().obs will use `Rx` as | 304 | /// For example, any custom "Model" class, like User().obs will use `Rx` as |
300 | /// wrapper. | 305 | /// wrapper. |
301 | class Rx<T> extends _RxImpl<T> { | 306 | class Rx<T> extends _RxImpl<T> { |
-
Please register or login to post a comment