1

I have an old side project that requires to be modified once in a while to match different customers needs. That said, It's not mandatory to me to update to latest version of frameworks since everything works just fine.

However in my last modification, when building the app flutter complains about my ScaffoldState.showSnackBar since it doesn't exist anymore. Doing some research I found that in order to achieve this now one have to use the ScaffoldMessenger class. Though, when implementing it, the class can't be found. So, now I can't use any of the methods and the app is broken. Again, I won't update to latest version now since changes are minors to the app. And, a couple months ago, it still worked fine.

Here is how I usually show my snackbar:

_loadSuccessSnackBar() {
    _scaffoldKey.currentState.showSnackBar(
      SnackBar(
        content: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              "Se han subido con exito los registros",
              style: TextStyle(
                color: Color.fromARGB(255, 186, 18, 57),
                fontSize: 15,
              ),
            ),
            SizedBox(width: 5),
            Icon(
              Icons.download_done_outlined,
              color: Colors.green,
            ),
          ],
        ),
      ),
    );
  }

where _scaffoldKey, is a Globalkey instance of type ScaffoldState:

final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();

Is there any workaround or solution to keep using this approach or update to newer versions that don't break the entire app? (I'm using flutter v2.7.0).

Just a note: debug building works just fine...

Thanks in advance for any help!

jraufeisen
  • 3,005
  • 7
  • 27
  • 43
yieniggu
  • 384
  • 1
  • 7
  • 20

1 Answers1

0

You have two options:

  1. Downgrade flutter to the version that you used to build your app with. Then your way of calling showSnackBar will still work.
  2. Migrate this part of your code as described in the migration guide.

The GlobalKey should now be declared like this

final GlobalKey<ScaffoldMessengerState> scaffoldMessengerKey = GlobalKey<ScaffoldMessengerState>();

and the ScaffoldMessenger can be constructed like this

ScaffoldMessenger(
  key: scaffoldMessengerKey,
  child: ...
)

You can then show the snackbar via

scaffoldMessengerKey.currentState.showSnackBar(mySnackBar);
jraufeisen
  • 3,005
  • 7
  • 27
  • 43
  • Thanks for the answer! Is there a way to check the build version I used, I can't remember since it never failed. I tried changing the key but it's not recognizing the class. Looks like something broke in between my last build (jan 23). However, if I run with debugging it works fine. Looks like it's a building issue. – yieniggu Apr 03 '23 at 18:19
  • "if I run with debugging it works fine" - Debug build works, but release build does not? This looks weird to me, potentially a problem with your flutter installation? There is a tool called "flutter version manager (fvm)" that could maybe help you with the version conflicts – jraufeisen Apr 03 '23 at 22:07