0

I have been learning flutter for sometime now. I am developing an application and I am stuck with a problem now.

                   showModalBottomSheet(
                       context: context,
                       builder: (context) {
                         return StatefulBuilder(
                            builder: (BuildContext context, StateSetter bottomsheetstate){
                         return buildSheet(context, script,bottomsheetstate);
                         });
                       },
                       isScrollControlled: true,
                       shape: RoundedRectangleBorder(
                           borderRadius: BorderRadius.vertical(
                               top: Radius.circular(15.0)
                           )
                       ),
                   ).whenComplete((){
                     SSEClient.unsubscribeFromSSE();
                   });
                 },

the whencomplete((){}); function worked when the builder was returning a regular widget (say a container) but when I changed it to return a stateful widget, the whencomplete((){}); function is not working anymore. I am puzzled by this. I want to know what is happening behind the scene and a solution to this problem.

thanks in advance!

1 Answers1

0

Edit: Michael Horn pointed out in a comment below that this will not work if an exception were thrown from the showModalBottomSheet, since the line debugPrint('hello world!'); would not run in that case.

I'm also not sure why your solution doesn't work in the first place. It works fine when I'm testing it, DartPad example here: https://dartpad.dev/?id=91673c44c26e6f01a117661fc1a904cd


You can await it, and then do what you want afterwards, like this:

    await showModalBottomSheet<void>(
            context: context,
            builder: (BuildContext context) {
             ...
            },
          );

   debugPrint('hello world!');

    

Notice the <void> at the end of showModalBottomSheet<void>, that means you can have the modal return a value if you want. Like for example using: showModalBottomSheet<bool> and Navigator.pop(context, true);

Here's a complete example:

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: _title,
      home: Scaffold(
        appBar: AppBar(title: const Text(_title)),
        body: const MyStatelessWidget(),
      ),
    );
  }
}

class MyStatelessWidget extends StatelessWidget {
  const MyStatelessWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Center(
      child: ElevatedButton(
        child: const Text('showModalBottomSheet'),
        onPressed: () async {
          await showModalBottomSheet<void>(
            context: context,
            builder: (BuildContext context) {
              return Container(
                height: 200,
                color: Colors.amber,
                child: Center(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    mainAxisSize: MainAxisSize.min,
                    children: <Widget>[
                      const Text('Modal BottomSheet'),
                      ElevatedButton(
                        child: const Text('Close BottomSheet'),
                        onPressed: () => Navigator.pop(context),
                      )
                    ],
                  ),
                ),
              );
            },
          );
          
          debugPrint('hello world!');
        },
      ),
    );
  }
}
Daniel Duvanå
  • 260
  • 4
  • 10
  • 1
    This is not quite the same - OP's code called `whenComplete` to do the unsubscription. The equivalent async/await solution would perform the subscription cancellation within the `finally` block of a `try-catch-finally`. This code would not unsubscribe if an exception were thrown from the `showModalBottomSheet` call – Michael Horn May 24 '22 at 15:13
  • Good point! Didn't think of that. – Daniel Duvanå May 24 '22 at 15:18
  • 1
    Thanks. It was more of an internal code issue . I figured it out. There is no issue with the whencomplete() function it just works fine.but thanks again!. – Mohamed zindha May 24 '22 at 16:08