Comments (7)
I have a page that will perform a HTTP Post when a button is pressed. That button will call an async function to perform the actual HTTP Post which could take a second or two. When I get a success or failure back I'm attempting to navigate back to the previous page, but it doesn't do anything.
No error was reported, it just fails silently.
I found a work around, just call the Get.back() function inside the button event:
onPressed: () {saveClicked(); Get.back();},Flutter Version:
Flutter 1.12.13+hotfix.9 • channel stable • https://github.com/flutter/flutter.git
Framework • revision f139b11009 (3 weeks ago) • 2020-03-30 13:57:30 -0700
Engine • revision af51afceb8
Tools • Dart 2.7.2Get Version:
get: ^1.17.2Describe on which device you found the bug:
Samsung Galaxy S8
Can you provide a Mock of the problem reproduction code? I have 3 applications in production that after the http request is sent, the app returns to the home page, and everything is working normally.
I believe that this is not related to the asynchronous function, but Get.back() should be in a place where it is never called.
Before providing your code (you can hide the API address), just test this:
onPressed: async () {saveClicked(); Get.back();},
If everything works normally, it means that there are no problems with asynchronous operations, but Get is not being called by your code.
Put a:
print("before get");
Get.back();
print("after get");
And you will know exactly what is going on. If the first print is not called, it is because your code is not even entering there.
from getx.
It was certainly being called, I had a break point on the Get.back() command and it was being hit.
I tried modifying the onPressed: to be async like you mentioned and Get.back() worked.
I did some more debugging and Get.back() only fails after I perform an await on my HTTP Post request.
I also added the print statements, and I get them both, but the page doesn't go back.
I'll try to get a minimal version together to reproduce it.
Thanks
from getx.
I tried anyway, I couldn't reproduce the error. He is apparently working properly.
void main() {
runApp(MaterialApp(
title: 'Test async call',
navigatorKey: Get.key,
home: TestScreen(),
));
}
class API {
static Future<dynamic> getTestError() async {
final response = await Dio().get('https://www.google.com');
if (response.statusCode == 200) {
print(response);
Get.back();
return response;
} else {
// Now you can made this
print('error on API');
Get.back();
}
}
}
class TestScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Container(
child: FlatButton(
color: Colors.blue,
child: Text('go to nextroute'),
onPressed: () {
Get.to(TestScreenTwo());
},
),
),
),
);
}
}
class TestScreenTwo extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Container(
child: FlatButton(
color: Colors.red,
child: Text('call api'),
onPressed: () {
API.getTestError();
},
),
),
),
);
}
}
from getx.
Okay, I've replicated the issue using a minor change to your sample code, it took me a while to figure out. If you put a Get.snackbar just before your Dio().get call it doesn't go back.
class API {
static Future<dynamic> getTestError() async {
Get.snackbar("title", "message");
final response = await Dio().get('https://www.google.com');
if (response.statusCode == 200) {
print(response);
Get.back();
return response;
} else {
// Now you can made this
print('error on API');
Get.back();
}
}
}
from getx.
Well, wouldn't the Get.back close the Snackbar in that situation?
from getx.
class API {
static Future getTestError() async {
Get.snackbar("title", "message");
final response = await Dio().get('https://www.google.com');
if (response.statusCode == 200) {
print(response);
Get.back();
return response;
} else {
// Now you can made this
print('error on API');
Get.back();
}
}
}
This behavior is expected, Get allows you to close a snackbar, dialog or bottomsheet dynamically, from anywhere.
If any of these are open, they will be closed, so you would need another Get.back(); to close, right?
Ideally, you should control when a dialog, snackbar or bottomsheet is open. If you can't control this, you should create some logic like this:
if(Get.isSnackbarOpen || Get.isDialogOpen || Get.isBottomsheetOpen){
Get.close(2);
} else {
Get.back();
}
Or maybe (much safer - recommended)
if(Get.isSnackbarOpen || Get.isDialogOpen || Get.isBottomsheetOpen){
navigator.popUntil((route) {
return (!Get.isSnackbarOpen && !Get.isDialogOpen && !Get.isBottomsheetOpen);
});
}
Get.back();
Remembering that you need to add GetObserver to your MaterialApp to gain access to these features.
from getx.
Okay, that wasn't clear in your documentation. It just states 'To return to previous screen'.
Thanks for the clarification.
from getx.
Related Issues (20)
- Get.snackbar() doesn't work successfully when starting the app for the first time. HOT 3
- [GetMiddleware] RouteDecoder.fromRoute can't Jump
- Flutter web deepLink HOT 4
- Using Base URL for graphql requests doesn't work
- 你们的PopScope正常嘛,我这边canPop=false时在iOS上依然能够侧滑返回,android上倒是正常 HOT 8
- 能否说明下 Get5 的路由 HOT 1
- Flutter web Getx routing dynamic API extension for testing
- Got status 3xx with followRedirects is true
- KINDLY COMMUNICATE WITH COMMUNITY PLEASE ( dear developer) you are making us loose hope HOT 8
- RTL Icon Padding Issue in Get.rawSnackbar() HOT 1
- PosScope in IOS while using Getx Route management. HOT 6
- Get.to 为什么在第一次跳转的时候当前route没有过渡效果
- Flutter getx trparams (@ variables substitution) does not work for Hebrew locale. Reported in StackOverflow.com HOT 3
- get.snackbar's selfsize
- Hot reload does not work in Windows HOT 1
- getx update not trigger ui refresh in time
- RouterReportManager base on _current to delete Controller is Not a good idae HOT 3
- same page transition issue
- XMLHttpRequest error in get request (web ) HOT 1
- how get GetMaterialApp instance?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from getx.