I want to show SnackBar pop-up once per build of a certain stateful page. This is a Flutter web project. Any help will be appreciated.


Solution 1: Sanketh B. K

i guess this is what you meant

Stafeful Widget {
    Widget build() {
       showsnackbar()
       return someWIdget();
   }


Solution 2: Yeasin Sheikh

Basically, I am using .addPostFrameCallback it will show after the frame is build.

 _showSnackBar() {
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      ScaffoldMessenger.of(context)
          .showSnackBar(SnackBar(content: Text("SnackBar")));
    });
  }

And place _showSnackBar() inside initState show snackeBar once.

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

And place _showSnackBar() under build method to show snackeBar every build

  Widget build(BuildContext context) {
    _showSnackBar();
    ......


Solution 3: Delwinn

This is an alternative

bool snackBarShowed = false; in top-level code

I put this inside the build method

if (snackBarShowed) {
    } else {
      Future.delayed(Duration.zero, () {
        ScaffoldMessenger.of(context).showSnackBar(SnackBar(
            action: SnackBarAction(
              label: 'Ok',
              onPressed: () {
                ScaffoldMessenger.of(context).hideCurrentSnackBar();
              },
            ),
            backgroundColor: Colors.black87,
            content: Text(
              'Use top-left logo to move to previous screen',
              style: body.copyWith(
                color: Colors.white,
              ),
            )));
      });
      snackBarShowed = true;
    }