Jonny Borges

refactor bind create and fix scope problem

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 3
  4 +import '../../../../main.dart';
4 import '../../../routes/app_pages.dart'; 5 import '../../../routes/app_pages.dart';
5 import '../controllers/home_controller.dart'; 6 import '../controllers/home_controller.dart';
6 7
@@ -8,6 +9,8 @@ class HomeView extends GetView<HomeController> { @@ -8,6 +9,8 @@ class HomeView extends GetView<HomeController> {
8 const HomeView({Key? key}) : super(key: key); 9 const HomeView({Key? key}) : super(key: key);
9 @override 10 @override
10 Widget build(BuildContext context) { 11 Widget build(BuildContext context) {
  12 + final value = Get.find<Controller>();
  13 + print(value);
11 return GetRouterOutlet.builder( 14 return GetRouterOutlet.builder(
12 routerDelegate: Get.nestedKey(Routes.home), 15 routerDelegate: Get.nestedKey(Routes.home),
13 builder: (context) { 16 builder: (context) {
@@ -5,6 +5,7 @@ import '../controllers/product_details_controller.dart'; @@ -5,6 +5,7 @@ import '../controllers/product_details_controller.dart';
5 class ProductDetailsBinding extends Binding { 5 class ProductDetailsBinding extends Binding {
6 @override 6 @override
7 List<Bind> dependencies() { 7 List<Bind> dependencies() {
  8 + print('JDISOJDOSIJOI');
8 return [ 9 return [
9 Bind.create<ProductDetailsController>( 10 Bind.create<ProductDetailsController>(
10 (_) => ProductDetailsController( 11 (_) => ProductDetailsController(
@@ -76,14 +76,14 @@ class AppPages { @@ -76,14 +76,14 @@ class AppPages {
76 transition: Transition.cupertino, 76 transition: Transition.cupertino,
77 showCupertinoParallax: true, 77 showCupertinoParallax: true,
78 participatesInRootNavigator: false, 78 participatesInRootNavigator: false,
79 - bindings: [ProductsBinding()], 79 + bindings: [ProductsBinding(), ProductDetailsBinding()],
80 children: [ 80 children: [
81 GetPage( 81 GetPage(
82 name: _Paths.productDetails, 82 name: _Paths.productDetails,
83 - transition: Transition.cupertino,  
84 - showCupertinoParallax: true, 83 + transition: Transition.cupertino,
  84 + showCupertinoParallax: true,
85 page: () => ProductDetailsView(), 85 page: () => ProductDetailsView(),
86 - bindings: [ProductDetailsBinding()], 86 + bindings: [],
87 middlewares: [ 87 middlewares: [
88 //only enter this route when authed 88 //only enter this route when authed
89 EnsureAuthMiddleware(), 89 EnsureAuthMiddleware(),
  1 +import 'package:example_nav2/services/auth_service.dart';
1 import 'package:flutter/material.dart'; 2 import 'package:flutter/material.dart';
2 import 'package:get/get.dart'; 3 import 'package:get/get.dart';
3 4
4 import 'app/modules/splash/controllers/splash_service.dart'; 5 import 'app/modules/splash/controllers/splash_service.dart';
5 import 'app/routes/app_pages.dart'; 6 import 'app/routes/app_pages.dart';
6 -import 'services/auth_service.dart'; 7 +
  8 +class Controller {}
7 9
8 void main() { 10 void main() {
9 runApp( 11 runApp(
10 GetMaterialApp( 12 GetMaterialApp(
11 title: "Application", 13 title: "Application",
12 binds: [ 14 binds: [
  15 + Bind.create<Controller>(
  16 + (_) => Controller(),
  17 + ),
13 Bind.put(SplashService()), 18 Bind.put(SplashService()),
14 Bind.put(AuthService()), 19 Bind.put(AuthService()),
15 ], 20 ],
@@ -20,6 +20,11 @@ class InstanceInfo { @@ -20,6 +20,11 @@ class InstanceInfo {
20 required this.isPrepared, 20 required this.isPrepared,
21 required this.isInit, 21 required this.isInit,
22 }); 22 });
  23 +
  24 + @override
  25 + String toString() {
  26 + return 'InstanceInfo(isPermanent: $isPermanent, isSingleton: $isSingleton, isRegistered: $isRegistered, isPrepared: $isPrepared, isInit: $isInit)';
  27 + }
23 } 28 }
24 29
25 extension Inst on GetInterface { 30 extension Inst on GetInterface {
@@ -463,6 +463,12 @@ class BindElement<T> extends InheritedElement { @@ -463,6 +463,12 @@ class BindElement<T> extends InheritedElement {
463 } 463 }
464 } 464 }
465 } else { 465 } else {
  466 + if (widget.create != null) {
  467 + _controllerBuilder = () => widget.create!.call(this);
  468 + Get.create<T>(_controllerBuilder!, tag: widget.tag, permanent: false);
  469 + } else {
  470 + _controllerBuilder = widget.init;
  471 + }
466 _controllerBuilder = 472 _controllerBuilder =
467 (widget.create != null ? () => widget.create!.call(this) : null) ?? 473 (widget.create != null ? () => widget.create!.call(this) : null) ??
468 widget.init; 474 widget.init;
@@ -74,13 +74,20 @@ class _GetCache<S extends GetLifeCycleMixin> extends WidgetCache<GetWidget<S>> { @@ -74,13 +74,20 @@ class _GetCache<S extends GetLifeCycleMixin> extends WidgetCache<GetWidget<S>> {
74 void onInit() { 74 void onInit() {
75 info = Get.getInstanceInfo<S>(tag: widget!.tag); 75 info = Get.getInstanceInfo<S>(tag: widget!.tag);
76 76
  77 + print('info: $info');
  78 +
77 _isCreator = info!.isPrepared && info!.isCreate; 79 _isCreator = info!.isPrepared && info!.isCreate;
78 80
79 if (info!.isRegistered) { 81 if (info!.isRegistered) {
80 _controller = Get.find<S>(tag: widget!.tag); 82 _controller = Get.find<S>(tag: widget!.tag);
81 } 83 }
82 84
  85 + print('controller: $_controller');
  86 + print('widget: $widget');
  87 +
83 GetWidget._cache[widget!] = _controller; 88 GetWidget._cache[widget!] = _controller;
  89 + print('cache: ${GetWidget._cache}');
  90 +
84 super.onInit(); 91 super.onInit();
85 } 92 }
86 93