Code Monkey home page Code Monkey logo

Comments (9)

egortabula avatar egortabula commented on September 26, 2024

Hi!
Did you use Bindings when you call Get.toNamed(AppRoutes.home); on screen A? If you use bindings, pass you MainLogic controller in bindings like that

class HomeBindings extends Bindings {
  @override
  void dependencies() {
    Get.put(MainLogic());
  }
}

and pass your Binding in GetPage

or if you don't want use Binding
you need to call Get.put before use call Get.find

For example you can init your controller automatically with GetBuilder

class MainWidget extends GetView<MainLogic> {
  const MainWidget({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Main"),
      ),
      body: InkWell(
        onTap: () {
          controller.changeContent();
        },
        child: SizedBox(
          width: double.infinity,
          height: double.infinity,
          child: Center(
            child: GetBuilder<MainLogic>(
              init: MainLogic(), //HERE!!
              assignId: true,
              builder: (logic) {
                return Text(controller.content);
              },
            ),
          ),
        ),
      ),
    );
  }
}

or you can call Get.put in build method before returning you widgets

class MainWidget extends GetView<MainLogic> {
  const MainWidget({super.key});

  @override
  Widget build(BuildContext context) {
    Get.put(MainLogic()); //HERE;

    return Scaffold(
      appBar: AppBar(
        title: const Text("Main"),
      ),
      body: // YOUR UI
    );
  }
}

from getx.

wen14701083 avatar wen14701083 commented on September 26, 2024

Yes, I used bindings, this is only possible if B quickly jumps to B again when B returns to A, other scenarios are normal.

class SplashBinding extends Bindings {
  @override
  void dependencies() {
    Get.lazyPut(() => SplashLogic());
  }
}

class MainBinding extends Bindings {
  @override
  void dependencies() {
    Get.lazyPut(() => MainLogic());
  }
}

class AppPages {
  static const initial = AppRoutes.initial;

  static final List<GetPage> getPages = [
    GetPage(
      name: AppRoutes.initial,
      page: () => SplashWidget(),
      binding: SplashBinding(),
    ),
    GetPage(
      name: AppRoutes.home,
      page: () => MainWidget(),
      binding: MainBinding(),
    ),
  ];
}

lib.zip

from getx.

egortabula avatar egortabula commented on September 26, 2024

Hello! Yes, I also caught an error, but in order to catch it, I need to very quickly press the button and return to screen B before Get has time to call onDispose.

If in your case you are supposed to press so quickly, then two options come to mind.

  1. Just block the button on the screen And if your controller has not yet been removed from memory. For example, when tapping, you can check for dependencies in memory
class SplashWidget extends GetView<SplashLogic> {
  const SplashWidget({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Splash"),
      ),
      body: InkWell(
        onTap: () async {
          try {
            // Try to find your controller
            final mainLogic = Get.find<MainLogic>();

            // If it still in memory, do nothing
            return;
          } catch (e) {
            // If it is not in memory, push your route
            await Get.toNamed(AppRoutes.home);
          }
        },
        child: const SizedBox(
          width: double.infinity,
          height: double.infinity,
          child: Center(
            child: Text("Go home"),
          ),
        ),
      ),
    );
  }
}
  1. There is an option not to delete the controller from memory.
class MainBinding extends Bindings {
  @override
  void dependencies() {
    // Use permament `true` and Get will not remove your dependency from memory.
    Get.put(MainLogic(), permanent: true);
  }
}

from getx.

wen14701083 avatar wen14701083 commented on September 26, 2024

Um... After B returns to A, onDispose is called about 0.3~0.5s.
The first option requires an interception to be added before all page jumps;
The second option occupies memory all the time and is not very friendly.

Hope there's a better solution.
Such as reducing the latency of onDispose, or not removing onDispose when a new GetView is using controller.

from getx.

synstin avatar synstin commented on September 26, 2024

Quite a few users are seeing this error in apps that are currently in production.
Has this issue been fixed in GetX5?

from getx.

egortabula avatar egortabula commented on September 26, 2024

Quite a few users are seeing this error in apps that are currently in production. Has this issue been fixed in GetX5?

Hello! No, this bug was not fixed in Getx 5.0. But I got the impression that in Get 5.0 I still catch it less often than in v4.6.6

from getx.

andrepurnomo avatar andrepurnomo commented on September 26, 2024

Hi, you can try set binding parameter in Get.to(), that error comes when you set binding in bindings file only. And maybe you need to set 'preventDuplicates' to prevent not opening the widget with the same tree

Get.to(
  () => widget,
  preventDuplicates: false,
  binding: BindingsBuilder.put(controller, tag: tag),
);

from getx.

synstin avatar synstin commented on September 26, 2024

@egortabula

Is it possible to check with isRegistered instead of catching the error with find?

from getx.

lwq1365102044 avatar lwq1365102044 commented on September 26, 2024

I have also encountered this problem. When I quickly enter and exit the page, this error will be caused. I try to modify the code by adding a mixin GetReferenceMixin to GetInstance. As long as GetBuilder is not released, GetxController will not be released.Reference code

from getx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.