Share
Embed
Deep Dive into Flutter State
Introduction to State and State Management in Flutter
Adi Nugroho
September 23, 2019
Transcript
21 slides
-
Adi Nugroho - Mobile Developer @ Upwork - Clients :
- Indonesia Stock Exchange - Edgeworks Solutions Pte. Ltd. (Singapore) - TestFairy (USA) - Synapsyte Pty. Ltd. (Australia) - Plantas Técnicas Plantec Cía. Ltda. (Ecuador) -
Agenda - Brief Introduction to Flutter - State in Flutter
- State Management -
What’s Flutter Cross platform framework developed by Google. Its main
programming language is Dart. It can be used to develop app on: - Android - iOS - Web - Desktop - Fuchsia (soon) -
What’s Flutter Everything related to view in Flutter is called
Widget. - Button - Text - Padding - Container -
Flutter Uses Reactive Programming - In imperative programming, we use
statement to change program’s state. btnConfirm.setOnClickListener { txtGreeting.text = txtInput.text } // What happen if txtGreeting or txtInput is not exist -
Flutter Uses Reactive Programming - In reactive programming, we change
the data flow / state and the changes will propagate to the widget. But it also incorporate many paradigms : https://flutter.dev/docs/resources/faq#what-programming-paradigm-does-flutters-framework-use -
class MyHomePage extends StatefulWidget { ... } class _MyHomePageState extends
State<MyHomePage> { var judul = "Hit You With"; void _onButtonPressed() { setState(() { judul = "Ddu Ddu Ddu"; }); } @override Widget build(BuildContext context) { return Column( children: <Widget>[ Text(judul,), MaterialButton( onPressed: () => _onButtonPressed(), ), ], ); } } -
How Flutter build() works - UI refreshes at 60fps. -
When a state change happens, Flutter engine compares between new and old. - Only update widgets that have state changes. - Old widget then get garbage collected. -
Widget and State Flutter has 2 type of widget -
Stateless Widget - Stateful Widget What is state? In Flutter, this is just a variable. -
Stateless Widget - Widget without state. - When it is
displayed, it execute the build method and DONE. - That’s it. - Best for static content and container. -
class StateDemo3 extends StatelessWidget { @override Widget build(BuildContext context) {
return Scaffold( appBar: AppBar( title: Text("Demo Stateless"), ), body: Center( child: Text( "Aplikasi Versi 1.0", ), ), ); } } -
Stateful Widget - Widget with state. - Best for widget
with dynamic content. - Has setState function to change state. - Has initState method that’s called when the widget is displayed. -
class StateDemo3 extends StatefulWidget { @override _StateDemo3State createState() => _StateDemo3State();
} class _StateDemo3State extends State<StateDemo3> { var versi = "0.0"; Future<void> _loadVersi() async { var loaded = await API.loadAppVersion(); setState(() { versi = loaded }); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text("Demo Stateless"), ), body: Center( child: Text( "Aplikasi Versi $versi", ), ), ); } -
Stateless Widget as Container Stateless Widget Stateful Widget Stateful Widget
Stateful Widget Stateless Widget -
Stateful Widget as Container? - Only when there’s a need
to communicate between children. - But there’s a better way to do this : State Management -
State Management - Almost every Flutter app consists of reusable
widgets. - These widgets need to communicate with each other, whether in one Container or when they’re being navigated. - Famous state management in Flutter: - BLoC - ScopedModel - Flutter Redux - Provider -
Provider - Built by Flutter team. Provider was created after
they need to combine the strength between ScopedModel and BLoC (Stream). - https://www.youtube.com/watch?v=d_m5csmrf7I&t=35s