Skip to content

Commit d9efb86

Browse files
committed
feat: adding a v2 of the controller without mixin
1 parent 697669a commit d9efb86

File tree

4 files changed

+71
-15
lines changed

4 files changed

+71
-15
lines changed

lib/binding/initial_binding.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'package:get/get.dart';
22
import 'package:getx_websocket_example/controller/home_controller.dart';
3-
import 'package:getx_websocket_example/controller/user_data_profile.controller.dart';
3+
import 'package:getx_websocket_example/controller/user_data_profile.controller_v1.dart';
4+
import 'package:getx_websocket_example/controller/user_data_profile.controller_v2.dart';
45
import 'package:getx_websocket_example/services/data.service.dart';
56
import 'package:getx_websocket_example/services/logging.service.dart';
67

@@ -11,7 +12,8 @@ class InitialBinding extends Bindings {
1112
void dependencies() {
1213
logger.i("InitialBinding");
1314
Get.put<DataService>(DataService());
14-
Get.put<UserDataProfileController>(UserDataProfileController());
15+
Get.put<UserDataProfileControllerV1>(UserDataProfileControllerV1());
16+
Get.put<UserDataProfileControllerV2>(UserDataProfileControllerV2());
1517
Get.put<HomeController>(HomeController());
1618
}
1719
}

lib/controller/user_data_profile.controller.dart renamed to lib/controller/user_data_profile.controller_v1.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import 'package:get/get.dart';
22
import 'package:getx_websocket_example/model/user-data-profile.entity.dart';
33
import 'package:getx_websocket_example/services/data.service.dart';
44

5-
class UserDataProfileController extends GetxController with StateMixin<UserDataProfile> {
5+
class UserDataProfileControllerV1 extends GetxController with StateMixin<UserDataProfile> {
66
final dataService = Get.find<DataService>();
77
Worker getxWorker;
88

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import 'package:get/get.dart';
2+
import 'package:getx_websocket_example/model/user-data-profile.entity.dart';
3+
import 'package:getx_websocket_example/services/data.service.dart';
4+
5+
enum Status { loading, error, success }
6+
7+
class UserDataProfileControllerV2 extends GetxController {
8+
final Rx<UserDataProfile> data = UserDataProfile().obs;
9+
final status = Status.loading.obs;
10+
final dataService = Get.find<DataService>();
11+
Worker getxWorker;
12+
13+
void change(UserDataProfile _profile, {Status newStatus}) {
14+
data(_profile);
15+
status(newStatus);
16+
}
17+
18+
void fetchData() {
19+
getxWorker = ever(dataService.fetchUserDataProfile(), (_profile) {
20+
if (_profile.uuid != null) {
21+
change(_profile, newStatus: Status.success);
22+
} else {
23+
change(null, newStatus: Status.loading);
24+
}
25+
});
26+
}
27+
28+
@override
29+
void onClose() {
30+
getxWorker?.dispose();
31+
}
32+
33+
@override
34+
void onInit() {
35+
super.onInit();
36+
}
37+
}

lib/view/detail.dart

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,42 @@
11
import 'package:flutter/material.dart';
22
import 'package:get/get.dart';
3-
import 'package:getx_websocket_example/controller/user_data_profile.controller.dart';
3+
import 'package:getx_websocket_example/controller/user_data_profile.controller_v2.dart';
44
import 'package:getx_websocket_example/services/logging.service.dart';
55

6-
class DetailScreen extends StatelessWidget {
6+
class DetailScreen extends GetWidget<UserDataProfileControllerV2> {
77
final logger = LoggingService().logger;
88

99
@override
1010
Widget build(BuildContext context) {
1111
logger.v('DetailScreen 1.1');
12-
Get.find<UserDataProfileController>().fetchData();
12+
controller.fetchData();
1313
return Scaffold(
14-
appBar: AppBar(
15-
title: Get.find<UserDataProfileController>().obx((_profile) => Text('' + _profile.uuid), onLoading: Text('Loading ...')),
16-
leading: IconButton(
17-
icon: Icon(Icons.arrow_back),
18-
onPressed: () {
19-
Get.back();
20-
},
14+
appBar: AppBar(
15+
title: Obx(() {
16+
if (controller.status() == Status.loading) {
17+
return Text('Loading ...');
18+
} else {
19+
return Text(controller.data().name);
20+
}
21+
}),
22+
leading: IconButton(
23+
icon: Icon(Icons.arrow_back),
24+
onPressed: () {
25+
Get.back();
26+
},
27+
),
2128
),
22-
),
23-
);
29+
body: Obx(() {
30+
const style = TextStyle(fontSize: 24);
31+
if (controller.status() == Status.loading) {
32+
return Center(child: Text('Loading ...', style: style));
33+
} else {
34+
return Center(
35+
child: Text(
36+
controller.data().email,
37+
style: style,
38+
));
39+
}
40+
}));
2441
}
2542
}

0 commit comments

Comments
 (0)