-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
about controller closing behavior when route closed #818
Comments
Hi @liugangnhm, This is expected behavior. You must place Get.put as the first line of the import 'package:flutter/material.dart';
import 'package:get/get.dart';
void main() {
runApp(MyApp());
}
class Controller extends GetxController {}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return GetMaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: Home(),
);
}
}
class Home extends StatelessWidget {
@override
Widget build(context) {
final Controller c = Get.put(Controller());
return Scaffold(
// Use Obx(()=> to update Text() whenever count is changed.
appBar: AppBar(title: Text('Home')),
// Replace the 8 lines Navigator.push by a simple Get.to(). You don't need context
body: Center(
child: RaisedButton(
child: Text('to page 1'),
onPressed: () {
Get.to(Page1());
},
),
),
);
}
}
class Page1 extends StatelessWidget {
Page1({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
final Controller controller = Get.put(Controller(), tag: 'page1');
return Scaffold(
appBar: AppBar(
title: Text('page1'),
),
body: Center(
child: RaisedButton(
child: Text('to page2'),
onPressed: () {
Get.to(Page2());
},
),
),
);
}
}
class Page2 extends StatelessWidget {
Page2({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
final Controller controller = Get.put(Controller(), tag: 'page2');
return Scaffold(
appBar: AppBar(
title: Text('page2'),
),
);
}
} If you use named routes this is not necessary. |
Thanks for your quick reply. As you said, what happened when the Widget is rebuilt? multi-controller will be put into? I will have a try now. |
Here it is clearly explained |
fixed I will close the issue |
ATTENTION: DO NOT USE THIS FIELD TO ASK SUPPORT QUESTIONS. USE THE PLATFORM CHANNELS FOR THIS. THIS SPACE IS DEDICATED ONLY FOR BUGS DESCRIPTION.
Fill in the template. Issues that do not respect the model will be closed.
Describe the bug
controller closing behavior is not expected for me
Reproduction code
NOTE: THIS IS MANDATORY, IF YOUR ISSUE DOES NOT CONTAIN IT, IT WILL BE CLOSED PRELIMINARY)
example:
To Reproduce
Steps to reproduce the behavior:
to page 1
buttonto page 2
buttonExpected behavior
i put a new Controller in Home, a new Controller(tag:page1) in Page1 , a new Controller(tag:page2) in Page2.
when i pop Page2 to back to page1, i thought Controller(tag:page2) should be close and removed from memory, but with the log i saw, Controller(tag:page2) was not closed. Then same to the Controller(tag:page1).
look at the log:
Controller closing has one delay to route closing.
Is that expected?
Screenshots
If applicable, add screenshots to help explain your problem.
Flutter Version:
Getx Version:
get:
git:
url: https://github.com/jonataslaw/getx.git
ref: dev
Describe on which device you found the bug:
ex: Moto z2 - Android.
Minimal reproduce code
Provide a minimum reproduction code for the problem
The text was updated successfully, but these errors were encountered: