简体   繁体   中英

MaterialButton onPressed listener not work (Flutter)

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM