Back to articles
Flutter Anti-Pattern: How setState() Turns Your App Into a Slideshow
How-ToTools

Flutter Anti-Pattern: How setState() Turns Your App Into a Slideshow

via Dev.toAnton Samoylov

The Problem : Many Flutter developers overuse setState() , calling it even when variable changes don't affect the UI. Every unnecessary setState() is a potential drop from 60 FPS to 59 FPS in complex interfaces. Chapter 1: When setState Is Actually Unnecessary Let’s examine a classic example found in 80% of applications: data entry forms. Developers often write: class MyForm extends StatefulWidget { @override _MyFormState createState () = > _MyFormState (); } class _MyFormState extends State < MyForm > { String _username = '' ; String _password = '' ; @override Widget build ( BuildContext context ) { return Column ( children: [ TextField ( onChanged: ( value ) { setState (() { // ❌ UNNECESSARY! _username = value ; // This variable is NOT used in UI }); }, ), TextField ( onChanged: ( value ) { setState (() { // ❌ UNNECESSARY! _password = value ; // This variable is also NOT in UI }); }, ), ElevatedButton ( onPressed: () { // Send data to server _sendToServer ( _username , _password ); }

Continue reading on Dev.to

Opens in a new tab

Read Full Article
8 views

Related Articles