Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Controller Reinitialization Issue: onInit and onReady Not Triggered When Returning to a Page After Disposal #3292

Open
shenzhenMirren opened this issue Jan 21, 2025 · 0 comments
Assignees

Comments

@shenzhenMirren
Copy link

shenzhenMirren commented Jan 21, 2025

### Describe the bug
When navigating from Page A to Page B, Page B's view contains an asynchronous callback that calls methods on the controller. 

Upon leaving Page B and returning to Page A, the controller's `onDelete` method is triggered, and the controller is disposed as expected. 

However, due to the asynchronous callback in Page B still referencing the controller, it gets re-initialized. 

When navigating back to Page B again, the controller already exists, and `onInit` or `onReady` are not called, leading to unexpected behavior and missing initialization.

### Reproduction code  
```dart
import 'package:flutter/material.dart';
import 'package:flutter_async_view_test/home/home_controller.dart';
import 'package:get/get.dart';

class HomeView extends GetView<HomeController> {
  const HomeView({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: ElevatedButton(
          child: const Text('Next Route'),
          onPressed: () {
            Get.toNamed('/second', arguments: {"data": "hello"});
          },
        ),
      ),
    );
  }
}

import 'package:flutter/material.dart';
import 'package:flutter_async_view_test/second/second_controller.dart';
import 'package:get/get.dart';

class SecondView extends GetView<SecondController> {
  const SecondView({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Second"),
      ),
      body: Center(
        child: Column(
          children: [
            Obx(() => Text("Get.arguments is: ${controller.data.value}")),
            ElevatedButton(
              onPressed: () async {
                Future.delayed(
                  const Duration(seconds: 3),
                  () {
                    debugPrint("There's a problem here...");
                    controller.doSomething();
                  },
                );
              },
              child: const Text("Click me and go back"),
            )
          ],
        ),
      ),
    );
  }
}

import 'package:flutter/material.dart';
import 'package:get/get.dart';

class SecondController extends GetxController {
  final data = "".obs;
  @override
  void onInit() {
    super.onInit();
    debugPrint("SecondController on init->${Get.arguments}");
    if (Get.arguments != null) {
      data.value = Get.arguments['data'] ?? '';
    }
  }

  void doSomething() {
    debugPrint("call do something");
  }
}

[lib.zip](https://github.com/user-attachments/files/18491859/lib.zip)

To Reproduce
Steps to reproduce the behavior:

  1. Run the app and start on Page A.
  2. Navigate to Page B.
  3. Leave Page B and return to Page A.
  4. Observe that the controller's onClose is triggered.
  5. Wait for the asynchronous task to complete, which re-initializes the controller.
  6. Navigate back to Page B.
  7. Observe that the controller's onInit and onReady methods are not called.

Expected behavior
When returning to Page B after the controller has been disposed, the controller should be fully re-initialized, including triggering onInit and onReady.

Flutter Version:
Flutter 3.24.5

Getx Version:
5.0.0-release-candidate-9.2.1

@jonataslaw jonataslaw added expected behavior it is not a bug and removed expected behavior it is not a bug labels Feb 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants