Jonny Borges

refactor bind create and fix scope problem

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../../../../main.dart';
import '../../../routes/app_pages.dart';
import '../controllers/home_controller.dart';
... ... @@ -8,6 +9,8 @@ class HomeView extends GetView<HomeController> {
const HomeView({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
final value = Get.find<Controller>();
print(value);
return GetRouterOutlet.builder(
routerDelegate: Get.nestedKey(Routes.home),
builder: (context) {
... ...
... ... @@ -5,6 +5,7 @@ import '../controllers/product_details_controller.dart';
class ProductDetailsBinding extends Binding {
@override
List<Bind> dependencies() {
print('JDISOJDOSIJOI');
return [
Bind.create<ProductDetailsController>(
(_) => ProductDetailsController(
... ...
... ... @@ -76,14 +76,14 @@ class AppPages {
transition: Transition.cupertino,
showCupertinoParallax: true,
participatesInRootNavigator: false,
bindings: [ProductsBinding()],
bindings: [ProductsBinding(), ProductDetailsBinding()],
children: [
GetPage(
name: _Paths.productDetails,
transition: Transition.cupertino,
showCupertinoParallax: true,
transition: Transition.cupertino,
showCupertinoParallax: true,
page: () => ProductDetailsView(),
bindings: [ProductDetailsBinding()],
bindings: [],
middlewares: [
//only enter this route when authed
EnsureAuthMiddleware(),
... ...
import 'package:example_nav2/services/auth_service.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'app/modules/splash/controllers/splash_service.dart';
import 'app/routes/app_pages.dart';
import 'services/auth_service.dart';
class Controller {}
void main() {
runApp(
GetMaterialApp(
title: "Application",
binds: [
Bind.create<Controller>(
(_) => Controller(),
),
Bind.put(SplashService()),
Bind.put(AuthService()),
],
... ...
... ... @@ -20,6 +20,11 @@ class InstanceInfo {
required this.isPrepared,
required this.isInit,
});
@override
String toString() {
return 'InstanceInfo(isPermanent: $isPermanent, isSingleton: $isSingleton, isRegistered: $isRegistered, isPrepared: $isPrepared, isInit: $isInit)';
}
}
extension Inst on GetInterface {
... ...
... ... @@ -463,6 +463,12 @@ class BindElement<T> extends InheritedElement {
}
}
} else {
if (widget.create != null) {
_controllerBuilder = () => widget.create!.call(this);
Get.create<T>(_controllerBuilder!, tag: widget.tag, permanent: false);
} else {
_controllerBuilder = widget.init;
}
_controllerBuilder =
(widget.create != null ? () => widget.create!.call(this) : null) ??
widget.init;
... ...
... ... @@ -74,13 +74,20 @@ class _GetCache<S extends GetLifeCycleMixin> extends WidgetCache<GetWidget<S>> {
void onInit() {
info = Get.getInstanceInfo<S>(tag: widget!.tag);
print('info: $info');
_isCreator = info!.isPrepared && info!.isCreate;
if (info!.isRegistered) {
_controller = Get.find<S>(tag: widget!.tag);
}
print('controller: $_controller');
print('widget: $widget');
GetWidget._cache[widget!] = _controller;
print('cache: ${GetWidget._cache}');
super.onInit();
}
... ...