Flutter: Send data to parent page & Reload - flutter

I'm new to flutter but I'm in love with it! :D
I'm doing a small application. It requires reloading the parent page when coming back from the child page.
I'm using Navigator.push() method to move between pages. I want to reload the parent page after calling Navigator.pop() method.
How can I do that?
Thanks,
-Naveed

On first widget
onPressed:() async {
var value = await Navigator.pushNamed(context,"/second");
setState(){
print(value);
}
}
on second widget
Navigator.pop(context, param); //pass parameter here

Related

Flutter dispose provider notifyListners()

I have a simple controller like this
class MatchListController with ChangeNotifier {
List<MatchData> liveMatches = [];
MatchListController() {
getMatchList();
}
getMatchList() async {
debugPrint('match list api called');
var response = await ApiService().matchList();
if (response != null) {
final databody = json.decode(response);
notifyListeners();
}
}
}
When my page loads it runs the function getMatchList() which is fine but If I change the page between function and when it return data notifyListeners hit and app crash. I am using loading but I have tab menu so I can change the page from there What I want is if page is close then notifyListeners dispose.
The answer that khalil mentioned is correct, there is also another solution:
Since you are using loading, I assume you want to prevent the page from changing, so you can wrap the tab menu buttons with an AbsorbPointer widget, and use setState to change its absorbing state according to your loading state. Your loading state can simply be a boolean inside your stateful widget.

Flutter call setState after Get.back()

I need to reload page after Get.back(). How to do it? How to call any method on previous page as soon as possible after using Get.back()?
For example:
I'm on Page1
Go to Page2
Use Get.back(), so user back to Page1
Usually user now see Page1 exactly like when he leave Page1. How to reload now view Page1?
Maybe is any method to override what I can call every time after navigate one page back? This method must come from Page1, not from Page2
you can do await while navigating to page 2.
Example:
await Get.to(()=> Page2());
setState({
// perform update
});
the setState will get fired once you come back to page 1 from page 2
another, similar example, code from Page1:
ListTile(
title: const Text("Item"),
onTap: () async {
await Get.toNamed(page2);
setState(() {});
}),
Using this sample, refresh (setState) fired after return from Page2 to Page1.
on Page 2: You need to write this
Get.back(result: true);
on Page 1: wait for response
var response = await Get.to(()=> Page2());
if(response){setState({
// perform update
});}

Flutter GetX refresh page after Get.back() [duplicate]

I need to reload page after Get.back(). How to do it? How to call any method on previous page as soon as possible after using Get.back()?
For example:
I'm on Page1
Go to Page2
Use Get.back(), so user back to Page1
Usually user now see Page1 exactly like when he leave Page1. How to reload now view Page1?
Maybe is any method to override what I can call every time after navigate one page back? This method must come from Page1, not from Page2
you can do await while navigating to page 2.
Example:
await Get.to(()=> Page2());
setState({
// perform update
});
the setState will get fired once you come back to page 1 from page 2
another, similar example, code from Page1:
ListTile(
title: const Text("Item"),
onTap: () async {
await Get.toNamed(page2);
setState(() {});
}),
Using this sample, refresh (setState) fired after return from Page2 to Page1.
on Page 2: You need to write this
Get.back(result: true);
on Page 1: wait for response
var response = await Get.to(()=> Page2());
if(response){setState({
// perform update
});}

How to call a function when returning to view/controller with `Get.back()`?

I am switching from the home view to another using Get.toNamed(Routes.DETAIL). When I want to return from the details view to the home view, I am calling Get.back() (or the user is using the back button of the devices).
Back on the home view, I would like to fetch all data from my database again.
Is there any function that is triggered when I am leaving a few and returning to it, so I can put my logic there?
Thank you
I would suggest you to use Get.offNamed() instead of Get.toNamed() as the offNamed() function will clear the data stored in catch and thus will again call the API declared in onInit() or onReady() lifecycle when returning back to that screen.
In Getx they have a funtion like
Get.back(result:"result");
so in order to trigger some funtion when going back to any page route
try doing this as the document written
final gotoHome = await Get.toNamed(Route.name); // or use the simple one Get.to(()=> Home());
then if you trigger to go back in page you should indicate some result e.g.
from back button in phone using willpopscope or a back button in UI.
Get.back(result:"triggerIt"); // this result will pass to the home.
so in will use
// It depend on you on where you gonna put this
// onInit or onReady or anything that would trigger
someTrigger() async{
final gotoHome = await Get.toNamed(Route.name);
if(gotoHome == "triggerIt"){
anyFuntionYouwantoTrigger();
}
}
for more info about it try to read the documentation.
https://github.com/jonataslaw/getx/blob/master/documentation/en_US/route_management.md
Edited: // Maybe some answer will pop up for better
I do have one but it's not that quite a real practice just a sample
e.g // sample you are now in the current page and this page is also connected to homecontroller or using Get.find() it need to bind the controller to the page;
class BindingHome with Bindings{
#override
void dependencies() {
Get.lazyPut(() => HomeController(), fenix: true);
}
}
then from GetPage add Binding
GetPage(
name: "/currentpage",
binding: BindingHome(),
page:() => HomeView(),
),
so while homecontroller is bind to the current page you are now so
// lets assume this one is put to the CurrentController
final homeController = Get.find<HomeController>();
so while calling back button on ui or willpopscope
when back try to trigger the function from home
gotBackfunction(){
Get.back();
homeController.anyFuntionYouwantoTrigger();
}
No you can't really call a function when doing so.
You should be using callback function just before popping the view.
onBackClick() async {
Get.lazyPut<MainController>(
() => MainController(),
);
controller.allItems.refresh();
Get.back();
}
Here is an example how you can do it without any complications:
WillPopScope(
onWillPop: () async {
await onBackClick();
return Future(() => false);
},
child: Scaffold(
appBar: CustomAppBarWithBack(
title: "Appbar",
OnClickBack: onBackClick,
),
body: Widget(),
),
);

How can i empty a list in parent after back button is pressed and the user goes back to the parent from new material page route

I tried passing the parent as this in the pram and then call parent.parentList.clear();
but after doing so the the Navigator is not popping and neither the list is changing.
nyIdeas?
I tried passing an instance of a function from parent to child but but still the same problem the navigator does not pop.
return WillPopScope(
onWillPop: () {
parent.parentList.clear();
Navigator.pop(context);
}, child : code.....
I think you could do this by passing a function down to the child and then calling it before popping. Have a look at my answer here: Flutter - How to change state from inside another widget
Hope it helps!