In Flutter how do I await the initialization of a screen before immediately calling another function?
For further context, when I press a button I open a dialog box that contains a Camera Preview from the camera package with a container box stacked on top of the Camera Preview to act as a 'view finder'. I then want to call the RectGetter function from the rect_getter package to locate the coordinates on the screen of the container box, however I need to wait for the CameraPreview and container box to be constructed first.
Below is the camera dialog I call that I need to be constructed before calling the RectGetter Function:
cameraDialog() async {
await showDialog(
barrierDismissible: false,
context: context,
builder: (_) => AlertDialog(
insetPadding: EdgeInsets.all(5.0),
contentPadding: EdgeInsets.all(3.0),
content: FutureBuilder<void>(
future: _initializeControllerFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return Stack(
children: [
cameraPrevew and containers etc...
I am aware of this post: How to run code after some delay in Flutter?
All these solutions just call a delay timer, which I suppose would work, however I'm looking for a more dynamic solution.
CodePudding user response:
You could use
https://github.com/slightfoot/flutter_after_layout
which executes a function only one time after the layout is completed. Or just look at its implementation and add it to your code :-)
Which is basically
void initState() {
super.initState();
WidgetsBinding.instance
.addPostFrameCallback((_) => yourFunction(context));
}
CodePudding user response:
I think what you're looking for is to call two functions simultaneously rather than waiting for one of them to complete and then start the other. If that is what you desire you need to remove async and await when you call or initialize both the functions.. like in the code snippet you need to remove async and await in the first two lines.
the code snippet that you have shared contains relatively less information regarding when and where these 2 functions are called and how they are called..!
