Open farukaziz opened 3 years ago
For now, you can use the WidgetEmpty class which prevents the server from sending any response until the returned widget changes Its state. Thus you have multiple options that will do the same thing and any widget that can change state will do the trick:
1. FutureBuilder
return FutureBuilder(
future: [YOUR ASYNC FUNCTION HERE],
builder: (context, snapshot) {
if (snapshot != null && snapshot.connectionState == ConnectionState.done) {
return [YOUR RESPONSE, It could be Json() or a widget];
} else {
// This will prevent the server from sending any response until your async function completes
return WidgetEmpty();
}
},
);
2. StatefulWidget
class MyRequestHandlingWidget extends StatefulWidget {
@override
_MyRequestHandlingWidgetState createState() => _MyRequestHandlingWidgetState();
}
class _MyRequestHandlingWidgetState extends State<MyRequestHandlingWidget> {
bool loaded = false;
@override
void initState() async {
await myAsyncFunction();
super.initState();
}
Future myAsyncFunction() async {
// My Async Code
// ...
// ...
// ...
setState(() {
loaded = true;
});
}
@override
Widget build(BuildContext context) {
if (loaded) {
// YOUR RESPONSE HERE
// return Json(.....);
// return Widget(....);
} else {
return WidgetEmpty();
}
}
}
3. The usual GetX state management solution
I like the
get_server
package but here I'm facing problem withasync
andawait
during response.