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.
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,
);
}
}
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(),
class TaskBindings extends Bindings{
@override
void dependencies() {
Get.put<TaskController>(TaskController());
}
}
class TaskController extends GetxController{
@override
void onInit() {
//todo: fetch Task info.
super.onInit();
}
}
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"),),
);
}
}
void navigate2NewTask() {
Get.toNamed(AppRoutes.newTask);
}
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.
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 ✅
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With