Can i show modal bottom sheet at app launch flutter?

Viewed 4198

Am trying to show bottom sheeet at app launch automatically in flutter, but it comes with errors. It only works for me when I instantiate it with a click event. But how can i pop it on screen launch?

 @override
  Widget build(BuildContext context) {
    showModalBottomSheet(context: context, builder: (BuildContext context) {
      return Container();
    });
2 Answers

you can do so in the initState of your first screen, like so

 @override
  void initState() {
    // TODO: implement initState

    Future.delayed(Duration(seconds: 0)).then((_) {
      showModalBottomSheet(
          context: context,
          builder: (builder) {
            return Container();
          });
    });
    super.initState();
  }


you need to do it like that, using future and delayed becuase initState dosent allow .of inside it, this is kinda hack but it works

Simple Solution

@override
  void initState() {
   
    super.initState();

    WidgetsBinding.instance.addPostFrameCallback((_) async {
      showModalBottomSheet(context: context, builder: (BuildContext context) {
      return Container();
    });
    });

  }
Related