0

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.

greenzebra
  • 412
  • 1
  • 5
  • 18

2 Answers2

1

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));
  }
Ruchit
  • 2,137
  • 1
  • 9
  • 24
  • Thanks! This does allow me to open my cameraDialog after initializing my screen. However I'm having trouble waiting for my cameraDialog to be initialized before I call the getRect function that locates where objects are on the cameraDialog screen. So in my initState() I call WidgetsBinding.instance! .addPostFrameCallback((_) => initCameraDialog(context)); which opens the cameraDialog great.... then... initCameraDialog(context) async { cameraDialog(context); WidgetsBinding.instance!.addPostFrameCallback((_) => getRect(context)); } this calls a null value – greenzebra Jan 16 '22 at 21:03
0

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..!

Yash Bhansali
  • 420
  • 2
  • 6
  • as stated in my question: "when I press a button I open a dialog box that contains a Camera Preview" ... which... "I need to be constructed before calling the RectGetter Function" First open and construct the dialog, then call a function that locates where objects are on the screen of the dialog. – greenzebra Jan 16 '22 at 21:09