4

I'm just navigating to new page using named routes, as soon as toNamed trigged new screen flashed, then closed console shows 'onDelete called', REPLACE ROUTE with navigating one.

  1. main.dart
class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.green,
      ),
      initialRoute: AppRoutes.splash,
      getPages: Pages.getPages,
    );
  }
}
  1. Pages
abstract class Pages {
  static List<GetPage> getPages = [
    GetPage(name: AppRoutes.splash, page: () => SplashPage(), binding: SplashBinding()),
    GetPage(name: AppRoutes.login, page: () => LoginPage(), binding: LoginBinding()),
    GetPage(name: AppRoutes.home, page: () => HomePage(), binding: HomeBinding(), transition: Transition.fadeIn),
    //issue in last one.
    GetPage(name: AppRoutes.newTask, page: () => NewTaskPage(), binding: TaskBindings(), transition: Transition.fadeIn),
  ];
}

NOTE: Navigating to NewTaskPage(),

  1. Binding
class TaskBindings extends Bindings{
  @override
  void dependencies() {
   Get.put<TaskController>(TaskController());
  }
}
  1. task controller
class TaskController extends GetxController{

@override
  void onInit() {
    //todo: fetch Task info.
    super.onInit();
  }

}
  1. Task Page
class NewTaskPage extends GetView<TaskController> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.red,
      appBar: AppBar(
        title: Text("NEW TASK"),
      ),
      body: Center(child: Text("No Working"),),
    );
  }
}
  1. Navigating function
void navigate2NewTask() {
    Get.toNamed(AppRoutes.newTask);
  }
  1. calling above function
class HomePage extends GetView<HomeController> {
  final GlobalKey<ScaffoldState> _homeScfKey = GlobalKey<ScaffoldState>();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        floatingActionButton: FloatingActionButton(
//SEE HERE
          onPressed: () => controller.navigate2NewTask(),
          child: Icon(Icons.add),
        ),
        key: _homeScfKey,
        drawer: HomeDrawer(),
)
}
}

New task page flashed and then the console shows this:

[GETX] GOING TO ROUTE /new-task
[GETX] Instance "TaskController" has been created
[GETX] Instance "TaskController" has been initialized
flutter: Splash navigation
[GETX] REPLACE ROUTE /new-task
[GETX] NEW ROUTE /app-home
flutter: Splash navigation
[GETX] "TaskController" onDelete() called
[GETX] "TaskController" deleted from memory

Please help.

techieasif
  • 153
  • 2
  • 9

1 Answers1

0

You should call Get.find on the very first screen on your app, so GetX will not delete it:

YourController yourController = Get.find<YourController>(); 

it worked for me ✅

Hamza Mihfad
  • 45
  • 1
  • 8