简体   繁体   中英

setState() for stateful widget

I want to setState my buttonbar widget in the other file. What can i do? -my code is true. I have any error. Just i want true setState.- But if i write DesktopHeader.desktopHeaderKey.currentState.,setState({}). it's null returning.

 class DesktopHeader extends StatefulWidget { static GlobalKey<State> desktopHeaderKey = GlobalKey(); const DesktopHeader({Key? key}): super(key: key); @override _DesktopHeaderState createState() => _DesktopHeaderState(); } class _DesktopHeaderState extends State<DesktopHeader> { @override Widget build(BuildContext context) { return butonBar(); } Widget butonBar() {... } }

When starting DesktopHeader, do you start it with key? In that:

DesktopHeader(key: DesktopHeader.desktopHeaderKey)

you can try return type DesktopHeader class

 final GlobalKey< State<DesktopHeader>> 
     globalKey = GlobalKey< State<DesktopHeader>>();

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