Ad

MaterialButton OnPressed Listener Not Work (Flutter)

- 1 answer

I created a layout like this, the problem is that the onPressed listener doesn't work but I can't understand why:

Material(
  shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(22.0)),
  color: const Color.fromRGBO(33, 63, 132, 1),
  clipBehavior: Clip.antiAlias,
  child: MaterialButton(
      height: 140.0,
      minWidth: 380.0,
      elevation: 20.0,
      color: const Color.fromRGBO(33, 63, 132, 1),
      textColor: Colors.white,
      child: Text("Chiudi sessione / Cambia utente",
          style: TextStyle(
            fontSize: ScreenUtil().setSp(20),
          )),
      onPressed: () {
        var future = endUserSession(badgeCode, false, context);
        future.then((value) {
          setUserBadgeForDualMode("");
          setDoorStatusIntoPrefs("");
          Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => const OpenDoorDualMode()));
        });
      }
      ),
)

What am I doing wrong?

Ad

Answer

The problem seems to be with your function call, you are not awaiting for a future value, Use it that way

var future = await endUserSession(badgeCode, false, context);

Or Try Removing it and call your function like this

   await endUserSession(badgeCode, false, context).then((value) async {
         await setUserBadgeForDualMode("");
         await setDoorStatusIntoPrefs("");
          Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => const OpenDoorDualMode()));
        });

Use "await" wherever you are expecting a future value.

Ad
source: stackoverflow.com
Ad