HACK TO UPDATE STATELESS WIDGET IN FLUTTER
class CustomWidget extends StatelessWidget {
CustomWidget ({super . key});
int counter = 0;
@override
Widget build (BuildContext context) {
return Scaffold(
floatingActionButton: Floating^ctionButton (
onPressed: () => _increment (context),
child: const Icon (Icons.plus_one),
),
body: Center (
child: Text("$counter"),
),
);
}
void _increment (BuildContext context) {
counter++;
(context as Element) .markNeedsBuild() ;
}
}
DO NOT USE THIS IN PRODUCTION
This works because every BuildContext inherits the Element class that has all the methods for rebuilding the ui.
You should not use this method in production otherwise you might encounter some weird bugs as Stateless widget is meant to be an immutable and it does not have lifecycle as Statefull widget.
0 Comments