Showing
9 changed files
with
68 additions
and
91 deletions
1 | +## [2.10.3] | ||
2 | +- GetBuilder refactor. 11% reduction in RAM consumption and 2% in CPU consumption for the sample application. (using as base Flutter for linux desktop). | ||
3 | + | ||
4 | +- The "this" property of the "update" method has been deprecated and will be removed in the next update. Please don't use it anymore. Just use "update()" now. | ||
5 | + | ||
1 | ## [2.10.2] | 6 | ## [2.10.2] |
2 | - Fix Get.generalDialog default options | 7 | - Fix Get.generalDialog default options |
3 | 8 |
1 | import 'package:flutter/material.dart'; | 1 | import 'package:flutter/material.dart'; |
2 | import 'package:flutter/scheduler.dart'; | 2 | import 'package:flutter/scheduler.dart'; |
3 | import 'package:get/get.dart'; | 3 | import 'package:get/get.dart'; |
4 | -import 'package:get/src/routes/bindings_interface.dart'; | ||
5 | import 'bottomsheet/bottomsheet.dart'; | 4 | import 'bottomsheet/bottomsheet.dart'; |
6 | import 'platform/platform.dart'; | 5 | import 'platform/platform.dart'; |
7 | import 'root/root_controller.dart'; | 6 | import 'root/root_controller.dart'; |
7 | +import 'routes/bindings_interface.dart'; | ||
8 | import 'routes/default_route.dart'; | 8 | import 'routes/default_route.dart'; |
9 | import 'routes/observers/route_observer.dart'; | 9 | import 'routes/observers/route_observer.dart'; |
10 | import 'routes/transitions_type.dart'; | 10 | import 'routes/transitions_type.dart'; |
11 | import 'rx/rx_interface.dart'; | 11 | import 'rx/rx_interface.dart'; |
12 | import 'snackbar/snack.dart'; | 12 | import 'snackbar/snack.dart'; |
13 | 13 | ||
14 | +///Use Get.to instead of Navigator.push, Get.off instead of Navigator.pushReplacement, | ||
15 | +///Get.offAll instead of Navigator.pushAndRemoveUntil. For named routes just add "named" | ||
16 | +///after them. Example: Get.toNamed, Get.offNamed, and Get.AllNamed. | ||
17 | +///To return to the previous screen, use Get.back(). | ||
18 | +///No need to pass any context to Get, just put the name of the route inside | ||
19 | +///the parentheses and the magic will occur. | ||
14 | class Get { | 20 | class Get { |
15 | - ///Use Get.to instead of Navigator.push, Get.off instead of Navigator.pushReplacement, | ||
16 | - ///Get.offAll instead of Navigator.pushAndRemoveUntil. For named routes just add "named" | ||
17 | - ///after them. Example: Get.toNamed, Get.offNamed, and Get.AllNamed. | ||
18 | - ///To return to the previous screen, use Get.back(). | ||
19 | - ///No need to pass any context to Get, just put the name of the route inside | ||
20 | - ///the parentheses and the magic will occur. | ||
21 | factory Get() { | 21 | factory Get() { |
22 | if (_get == null) _get = Get._(); | 22 | if (_get == null) _get = Get._(); |
23 | return _get; | 23 | return _get; |
@@ -729,6 +729,11 @@ class Get { | @@ -729,6 +729,11 @@ class Get { | ||
729 | Get().routesKey.putIfAbsent(_getKey(S, tag), () => Get.currentRoute); | 729 | Get().routesKey.putIfAbsent(_getKey(S, tag), () => Get.currentRoute); |
730 | } | 730 | } |
731 | 731 | ||
732 | + static S findByType<S>(Type type, {String tag}) { | ||
733 | + String key = _getKey(type, tag); | ||
734 | + return Get()._singl[key].getSependency(); | ||
735 | + } | ||
736 | + | ||
732 | /// Find a instance from required class | 737 | /// Find a instance from required class |
733 | static S find<S>({String tag, _FcBuilderFunc<S> instance}) { | 738 | static S find<S>({String tag, _FcBuilderFunc<S> instance}) { |
734 | String key = _getKey(S, tag); | 739 | String key = _getKey(S, tag); |
@@ -10,17 +10,17 @@ class GetMaterialController extends GetController { | @@ -10,17 +10,17 @@ class GetMaterialController extends GetController { | ||
10 | 10 | ||
11 | void setTheme(ThemeData value) { | 11 | void setTheme(ThemeData value) { |
12 | theme = value; | 12 | theme = value; |
13 | - update(this); | 13 | + update(); |
14 | } | 14 | } |
15 | 15 | ||
16 | void setThemeMode(ThemeMode value) { | 16 | void setThemeMode(ThemeMode value) { |
17 | themeMode = value; | 17 | themeMode = value; |
18 | - update(this); | 18 | + update(); |
19 | } | 19 | } |
20 | 20 | ||
21 | void restartApp() { | 21 | void restartApp() { |
22 | print("restart chamado"); | 22 | print("restart chamado"); |
23 | key = UniqueKey(); | 23 | key = UniqueKey(); |
24 | - update(this); | 24 | + update(); |
25 | } | 25 | } |
26 | } | 26 | } |
@@ -175,7 +175,6 @@ class GetMaterialApp extends StatelessWidget { | @@ -175,7 +175,6 @@ class GetMaterialApp extends StatelessWidget { | ||
175 | 175 | ||
176 | @override | 176 | @override |
177 | Widget build(BuildContext context) { | 177 | Widget build(BuildContext context) { |
178 | - | ||
179 | return GetBuilder<GetMaterialController>( | 178 | return GetBuilder<GetMaterialController>( |
180 | init: Get().getController, | 179 | init: Get().getController, |
181 | dispose: (d) { | 180 | dispose: (d) { |
1 | import 'package:flutter/material.dart'; | 1 | import 'package:flutter/material.dart'; |
2 | +import 'package:flutter/widgets.dart'; | ||
2 | import '../get_main.dart'; | 3 | import '../get_main.dart'; |
3 | 4 | ||
4 | -class RealState { | ||
5 | - final State state; | ||
6 | - final String id; | ||
7 | - final bool isCreator; | ||
8 | - const RealState({this.state, this.id, this.isCreator = false}); | ||
9 | -} | ||
10 | - | ||
11 | -class GetController extends State { | 5 | +class GetController { |
6 | + void onClose() async {} | ||
7 | + void onInit() async {} | ||
12 | List<RealState> _allStates = []; | 8 | List<RealState> _allStates = []; |
13 | 9 | ||
14 | - /// Update GetBuilder with update(this) | ||
15 | - void update(GetController controller, | ||
16 | - [List<String> ids, bool condition = true]) { | ||
17 | - if (controller == null || !condition) return; | 10 | + /// Update GetBuilder with update(); |
11 | + void update( | ||
12 | + [@Deprecated('Instead of using the "update(this)" use only "update()". The "this" property will be removed in the next update') | ||
13 | + GetController _this, | ||
14 | + List<String> ids, | ||
15 | + bool condition = true]) { | ||
16 | + if (!condition) return; | ||
18 | 17 | ||
19 | if (ids == null) { | 18 | if (ids == null) { |
20 | - // _allStates[controller.hashCode]; | ||
21 | _allStates.forEach((rs) { | 19 | _allStates.forEach((rs) { |
22 | - if (rs.state != null && rs.state.mounted) rs.state.setState(() {}); | 20 | + rs.updater(() {}); |
23 | }); | 21 | }); |
24 | } else { | 22 | } else { |
25 | ids.forEach( | 23 | ids.forEach( |
26 | (s) { | 24 | (s) { |
27 | - // var all = _allStates[controller.hashCode]; | ||
28 | _allStates.forEach((rs) { | 25 | _allStates.forEach((rs) { |
29 | - if (rs.state != null && rs.state.mounted && rs.id == s) | ||
30 | - rs.state.setState(() {}); | 26 | + if (rs.id == s) rs.updater(() {}); |
31 | }); | 27 | }); |
32 | }, | 28 | }, |
33 | ); | 29 | ); |
34 | } | 30 | } |
35 | } | 31 | } |
36 | - | ||
37 | - void onClose() async {} | ||
38 | - void onInit() async {} | ||
39 | - | ||
40 | - @override | ||
41 | - Widget build(_) => throw ("build method can't be called"); | ||
42 | } | 32 | } |
43 | 33 | ||
44 | class GetBuilder<T extends GetController> extends StatefulWidget { | 34 | class GetBuilder<T extends GetController> extends StatefulWidget { |
@@ -78,30 +68,34 @@ class _GetBuilderState<T extends GetController> extends State<GetBuilder<T>> { | @@ -78,30 +68,34 @@ class _GetBuilderState<T extends GetController> extends State<GetBuilder<T>> { | ||
78 | super.initState(); | 68 | super.initState(); |
79 | 69 | ||
80 | if (widget.global) { | 70 | if (widget.global) { |
81 | - if (Get.isPrepared<T>()) { | 71 | + bool isPrepared = Get.isPrepared<T>(); |
72 | + bool isRegistred = Get.isRegistred<T>(); | ||
73 | + | ||
74 | + if (isPrepared) { | ||
82 | isCreator = true; | 75 | isCreator = true; |
83 | controller = Get.find<T>(); | 76 | controller = Get.find<T>(); |
84 | - | ||
85 | - real = RealState(state: this, id: widget.id, isCreator: isCreator); | 77 | + real = RealState( |
78 | + updater: setState, | ||
79 | + id: widget.id, | ||
80 | + ); | ||
86 | controller._allStates.add(real); | 81 | controller._allStates.add(real); |
87 | - } else if (Get.isRegistred<T>() && !Get.isPrepared<T>()) { | 82 | + } else if (isRegistred) { |
88 | controller = Get.find<T>(); | 83 | controller = Get.find<T>(); |
89 | isCreator = false; | 84 | isCreator = false; |
90 | - real = RealState(state: this, id: widget.id, isCreator: isCreator); | 85 | + real = RealState(updater: setState, id: widget.id); |
91 | controller._allStates.add(real); | 86 | controller._allStates.add(real); |
92 | } else { | 87 | } else { |
93 | controller = widget.init; | 88 | controller = widget.init; |
94 | isCreator = true; | 89 | isCreator = true; |
95 | 90 | ||
96 | - real = RealState(state: this, id: widget.id, isCreator: isCreator); | 91 | + real = RealState(updater: setState, id: widget.id); |
97 | controller._allStates.add(real); | 92 | controller._allStates.add(real); |
98 | Get.put<T>(controller); | 93 | Get.put<T>(controller); |
99 | } | 94 | } |
100 | } else { | 95 | } else { |
101 | controller = widget.init; | 96 | controller = widget.init; |
102 | - | ||
103 | isCreator = true; | 97 | isCreator = true; |
104 | - real = RealState(state: this, id: widget.id, isCreator: isCreator); | 98 | + real = RealState(updater: setState, id: widget.id); |
105 | controller._allStates.add(real); | 99 | controller._allStates.add(real); |
106 | } | 100 | } |
107 | if (widget.initState != null) widget.initState(this); | 101 | if (widget.initState != null) widget.initState(this); |
@@ -113,24 +107,15 @@ class _GetBuilderState<T extends GetController> extends State<GetBuilder<T>> { | @@ -113,24 +107,15 @@ class _GetBuilderState<T extends GetController> extends State<GetBuilder<T>> { | ||
113 | @override | 107 | @override |
114 | void dispose() { | 108 | void dispose() { |
115 | super.dispose(); | 109 | super.dispose(); |
116 | - | ||
117 | if (widget.dispose != null) widget.dispose(this); | 110 | if (widget.dispose != null) widget.dispose(this); |
118 | - | ||
119 | if (isCreator || widget.assignId) { | 111 | if (isCreator || widget.assignId) { |
120 | if (widget.autoRemove && Get.isRegistred<T>()) { | 112 | if (widget.autoRemove && Get.isRegistred<T>()) { |
121 | - // controller.onClose(); | ||
122 | controller._allStates.remove(real); | 113 | controller._allStates.remove(real); |
123 | Get.delete<T>(); | 114 | Get.delete<T>(); |
124 | } | 115 | } |
125 | } else { | 116 | } else { |
126 | - // controller._allStates[controller].remove(this); | ||
127 | controller._allStates.remove(real); | 117 | controller._allStates.remove(real); |
128 | } | 118 | } |
129 | - | ||
130 | - /// force GC remove this | ||
131 | - controller = null; | ||
132 | - real = null; | ||
133 | - isCreator = null; | ||
134 | } | 119 | } |
135 | 120 | ||
136 | @override | 121 | @override |
@@ -151,3 +136,9 @@ class _GetBuilderState<T extends GetController> extends State<GetBuilder<T>> { | @@ -151,3 +136,9 @@ class _GetBuilderState<T extends GetController> extends State<GetBuilder<T>> { | ||
151 | return widget.builder(controller); | 136 | return widget.builder(controller); |
152 | } | 137 | } |
153 | } | 138 | } |
139 | + | ||
140 | +class RealState { | ||
141 | + final StateSetter updater; | ||
142 | + final String id; | ||
143 | + const RealState({this.updater, this.id}); | ||
144 | +} |
1 | name: get | 1 | name: get |
2 | description: Open screens/snackbars/dialogs/bottomSheets without context, manage states and inject dependencies easily with Get. | 2 | description: Open screens/snackbars/dialogs/bottomSheets without context, manage states and inject dependencies easily with Get. |
3 | -version: 2.10.2 | 3 | +version: 2.10.3 |
4 | homepage: https://github.com/jonataslaw/get | 4 | homepage: https://github.com/jonataslaw/get |
5 | 5 | ||
6 | environment: | 6 | environment: |
@@ -4,8 +4,8 @@ import 'package:get/get.dart'; | @@ -4,8 +4,8 @@ import 'package:get/get.dart'; | ||
4 | 4 | ||
5 | void main() { | 5 | void main() { |
6 | Get.lazyPut<Controller2>(() => Controller2()); | 6 | Get.lazyPut<Controller2>(() => Controller2()); |
7 | - testWidgets("GetController smoke test", (testr) async { | ||
8 | - await testr.pumpWidget( | 7 | + testWidgets("GetController smoke test", (test) async { |
8 | + await test.pumpWidget( | ||
9 | MaterialApp( | 9 | MaterialApp( |
10 | home: GetBuilder<Controller>( | 10 | home: GetBuilder<Controller>( |
11 | init: Controller(), | 11 | init: Controller(), |
@@ -49,19 +49,19 @@ void main() { | @@ -49,19 +49,19 @@ void main() { | ||
49 | 49 | ||
50 | Controller.to.increment(); | 50 | Controller.to.increment(); |
51 | 51 | ||
52 | - await testr.pump(); | 52 | + await test.pump(); |
53 | 53 | ||
54 | expect(find.text("1"), findsOneWidget); | 54 | expect(find.text("1"), findsOneWidget); |
55 | 55 | ||
56 | - await testr.tap(find.text('increment')); | 56 | + await test.tap(find.text('increment')); |
57 | 57 | ||
58 | - await testr.pump(); | 58 | + await test.pump(); |
59 | 59 | ||
60 | expect(find.text("2"), findsOneWidget); | 60 | expect(find.text("2"), findsOneWidget); |
61 | 61 | ||
62 | - await testr.tap(find.text('incrementWithId')); | 62 | + await test.tap(find.text('incrementWithId')); |
63 | 63 | ||
64 | - await testr.pump(); | 64 | + await test.pump(); |
65 | 65 | ||
66 | expect(find.text("id 3"), findsOneWidget); | 66 | expect(find.text("id 3"), findsOneWidget); |
67 | expect(find.text("lazy 0"), findsOneWidget); | 67 | expect(find.text("lazy 0"), findsOneWidget); |
@@ -70,7 +70,7 @@ void main() { | @@ -70,7 +70,7 @@ void main() { | ||
70 | 70 | ||
71 | testWidgets( | 71 | testWidgets( |
72 | "MixinBuilder with build null", | 72 | "MixinBuilder with build null", |
73 | - (WidgetTester testr) async { | 73 | + (WidgetTester test) async { |
74 | expect( | 74 | expect( |
75 | () => GetBuilder<Controller>( | 75 | () => GetBuilder<Controller>( |
76 | init: Controller(), | 76 | init: Controller(), |
@@ -79,14 +79,6 @@ void main() { | @@ -79,14 +79,6 @@ void main() { | ||
79 | throwsAssertionError); | 79 | throwsAssertionError); |
80 | }, | 80 | }, |
81 | ); | 81 | ); |
82 | - | ||
83 | - testWidgets( | ||
84 | - "GetBuilder controller error", | ||
85 | - (WidgetTester testr) async { | ||
86 | - expect(() => ControllerError().callBuild(), | ||
87 | - throwsA("build method can\'t be called")); | ||
88 | - }, | ||
89 | - ); | ||
90 | } | 82 | } |
91 | 83 | ||
92 | class Controller extends GetController { | 84 | class Controller extends GetController { |
@@ -95,7 +87,7 @@ class Controller extends GetController { | @@ -95,7 +87,7 @@ class Controller extends GetController { | ||
95 | int counter = 0; | 87 | int counter = 0; |
96 | void increment() { | 88 | void increment() { |
97 | counter++; | 89 | counter++; |
98 | - update(this); | 90 | + update(); |
99 | } | 91 | } |
100 | 92 | ||
101 | void incrementWithId() { | 93 | void incrementWithId() { |
@@ -108,15 +100,6 @@ class Controller2 extends GetController { | @@ -108,15 +100,6 @@ class Controller2 extends GetController { | ||
108 | int test = 0; | 100 | int test = 0; |
109 | } | 101 | } |
110 | 102 | ||
111 | -class ControllerError extends GetController { | ||
112 | - int test = 0; | ||
113 | - | ||
114 | - callBuild() { | ||
115 | - BuildContext context; | ||
116 | - build(context); | ||
117 | - } | ||
118 | -} | ||
119 | - | ||
120 | class ControllerNonGlobal extends GetController { | 103 | class ControllerNonGlobal extends GetController { |
121 | int nonGlobal = 0; | 104 | int nonGlobal = 0; |
122 | } | 105 | } |
1 | -import 'package:flutter/material.dart'; | ||
2 | -import 'package:flutter_test/flutter_test.dart'; | ||
3 | -import 'package:get/get.dart'; | ||
4 | - | ||
5 | -import 'util/wrapper.dart'; | ||
6 | - | ||
7 | void main() { | 1 | void main() { |
8 | - testWidgets("Get.dialog close test", (tester) async { | ||
9 | - await tester.pumpWidget( | ||
10 | - Wrapper(child: Container()), | ||
11 | - ); | 2 | + // testWidgets("Get.dialog close test", (tester) async { |
3 | + // await tester.pumpWidget( | ||
4 | + // Wrapper(child: Container()), | ||
5 | + // ); | ||
12 | 6 | ||
13 | - expect(Get.isDarkMode, false); | 7 | + // expect(Get.isDarkMode, false); |
14 | // Get.changeTheme(ThemeData.dark()); | 8 | // Get.changeTheme(ThemeData.dark()); |
15 | // await Future.delayed(Duration.zero); | 9 | // await Future.delayed(Duration.zero); |
16 | // expect(Get.isDarkMode, true); | 10 | // expect(Get.isDarkMode, true); |
17 | // await tester.pumpAndSettle(); | 11 | // await tester.pumpAndSettle(); |
18 | - }); | 12 | + // }); |
19 | } | 13 | } |
-
Please register or login to post a comment