Skip to content

Instantly share code, notes, and snippets.

@roipeker
Created May 28, 2021 01:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save roipeker/6b08e9f7e2d8b83ecbf399cbe039ca09 to your computer and use it in GitHub Desktop.
Save roipeker/6b08e9f7e2d8b83ecbf399cbe039ca09 to your computer and use it in GitHub Desktop.
Getx Issue 1481 (same Type Controllers in multipage navigation).
/// Note:
/// A simple fix for this case scenario of local controller lifecycle, might be
/// to separate the `global:false` logic here:
/// https://github.com/jonataslaw/getx/blob/07ad5d7f9e726f181800359d0218b44e5967ccf8/lib/get_state_manager/src/simple/get_state.dart#L177
// ...
// if (_isCreator! || widget.assignId) {
// if (widget.autoRemove) {
// /// if the Controller is flagged as "local", run manually `onClose()`.
// if( !widget.global ){
// controller?.onClose();
// } else if(GetInstance().isRegistered<T>(tag: widget.tag)){
// GetInstance().delete<T>(tag: widget.tag);
// }
// }
// }
// ...
/// The 2 samples here are forcing a unique Controller instance per Route, even if the View and Controller
/// are the same Type. Both approaches uses `GetBuilder()` as a Stateful initializer to manage our Controller lifecycle.
/// Sample 1 uses global registration with tag, Sample 2 uses a local controller (not accesible via Get.find()).
import 'package:flutter/material.dart';
import 'package:get/get.dart';
void main() {
runApp(MaterialApp(home: HomeView(), navigatorKey: Get.key));
}
/// Sample 1 (global controller with tags)
class HomeView extends StatelessWidget {
const HomeView({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
/// option 1 - Global instance with tag.
final _controller = _Controller();
return GetBuilder<_Controller>(
init: _controller,
tag: _controller.tag,
autoRemove: true,
builder: (_) => _HomeInternal(controller: _controller),
);
}
/// Sample 2 (local controller)
//
// class HomeView extends StatelessWidget {
// const HomeView({Key? key}) : super(key: key);
//
// @override
// Widget build(BuildContext context) {
// return GetBuilder<_Controller>(
// init: _Controller(),
// global: false,
// dispose: (state) => state.controller?.onClose(),
// builder: (controller) => _HomeInternal(controller: controller),
// );
// }
// }
class _HomeInternal extends StatelessWidget {
final _Controller controller;
const _HomeInternal({Key? key, required this.controller}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(controller.title)),
body: Center(
child: TextButton(onPressed: controller.nextPage, child: Text("Next")),
),
);
}
}
class _Controller extends GetxController {
static int _instanceCount = 0;
final int _id = ++_instanceCount;
late String tag;
_Controller() {
tag = 'tag$_id';
}
String get title => 'Home #$_id';
@override
void onReady() {
print('onready $title');
}
@override
void onClose() {
print('onclose $title');
}
void nextPage() {
Get.to(() => HomeView());
}
}
@dingjianjaja
Copy link

dingjianjaja commented Jul 26, 2022

Sample 2
If I don't use GetBuilder, "onClose" method will never be called.
May I ask what is the reason

class HomeInternal extends StatelessWidget {
  // const HomeInternal({Key? key, required this.controller}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final _Controller controller = _Controller();
    return Scaffold(
      appBar: AppBar(title: Text(controller.title)),
      body: Center(
        child: TextButton(onPressed: controller.nextPage, child: Text("Next")),
      ),
    );
  }
}

class _Controller extends GetxController {
  static int _instanceCount = 0;
  final int _id = ++_instanceCount;

  late String tag;

  _Controller() {
    tag = 'tag$_id';
  }

  String get title => 'Home #$_id';

  @override
  void onReady() {
    print('onready $title');
  }

  @override
  void onClose() {
    print('onclose $title');
  }

  void nextPage() {
    Get.to(() => YZTestGetXPage());
  }
}


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment