|
| 1 | +--- |
| 2 | +title: FlutterFlow Model |
| 3 | +slug: /generated-code/flutterflow-model |
| 4 | +sidebar_position: 4 |
| 5 | +--- |
| 6 | + |
| 7 | +# FlutterFlow Model |
| 8 | + |
| 9 | +The `FlutterFlowModel` class is an abstract class used in FlutterFlow to provide a unified and extensible structure for managing state and behavior of widgets (both pages and components). It encapsulates **initialization, state management,** and **disposal** logic, making it easier to handle the lifecycle of widgets and their models. |
| 10 | + |
| 11 | +FlutterFlow automatically generates the `flutter_flow_model.dart` file, which contains the `FlutterFlowModel` class and utility methods like `wrapWithModel()` and `createModel()`. |
| 12 | + |
| 13 | +The diagram below illustrates how these utility classes and methods are utilized in a widget or model class: |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | +When a component is added to your page (and every component you create [generates both a widget and a model class)](component-gen-code.md), the flow below explains how the utility classes are used when there is a child component: |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | +<p></p> |
| 23 | + |
| 24 | +Here’s a breakdown of the lifecycle of `FlutterFlowModel` class: |
| 25 | + |
| 26 | +## Initialization |
| 27 | +Ensures the model is initialized **only once** and is tied to the `BuildContext` and the widget it is associated with. |
| 28 | + |
| 29 | +```js |
| 30 | +abstract class FlutterFlowModel<W extends Widget> { |
| 31 | + // Initialization methods |
| 32 | + bool _isInitialized = false; |
| 33 | + void initState(BuildContext context); |
| 34 | + void _init(BuildContext context) { |
| 35 | + if (!_isInitialized) { |
| 36 | + initState(context); |
| 37 | + _isInitialized = true; |
| 38 | + } |
| 39 | + if (context.widget is W) _widget = context.widget as W; |
| 40 | + _context = context; |
| 41 | + } |
| 42 | +``` |
| 43 | +
|
| 44 | +
|
| 45 | +## Widget & Context references |
| 46 | +
|
| 47 | +Provides references to the associated widget and its `BuildContext`. |
| 48 | +
|
| 49 | +```js |
| 50 | + // The widget associated with this model. This is useful for accessing the |
| 51 | + // parameters of the widget, for example. |
| 52 | + W? _widget; |
| 53 | + W? get widget => _widget; |
| 54 | + |
| 55 | + // The context associated with this model. |
| 56 | + BuildContext? _context; |
| 57 | + BuildContext? get context => _context; |
| 58 | +``` |
| 59 | +
|
| 60 | +`_widget` and `_context` (private fields) store the widget and context references. `widget` and `context` (getters) are the public accessors for `_widget` and `_context`. |
| 61 | +
|
| 62 | +## Disposal |
| 63 | +
|
| 64 | +Manages the cleanup of resources when the model or widget is disposed. |
| 65 | +
|
| 66 | +```js |
| 67 | + bool disposeOnWidgetDisposal = true; |
| 68 | + void dispose(); |
| 69 | + void maybeDispose() { |
| 70 | + if (disposeOnWidgetDisposal) { |
| 71 | + dispose(); |
| 72 | + } |
| 73 | + // Remove reference to widget for garbage collection purposes. |
| 74 | + _widget = null; |
| 75 | + } |
| 76 | +``` |
| 77 | +The `disposeOnWidgetDisposal` determines whether the model should be disposed when the widget is removed. This defaults to `true` for **pages** and `false` for **components** (as parent models typically manage their child components). |
| 78 | +
|
| 79 | +The `maybeDispose()` checks `disposeOnWidgetDisposal` before disposing. It removes the widget reference to aid garbage collection. |
| 80 | +
|
| 81 | +## Updates and Change Notification |
| 82 | +
|
| 83 | +Allows the model to notify the associated widget or parent component/page when updates occur. |
| 84 | +
|
| 85 | +```js |
| 86 | + // Whether to update the containing page / component on updates. |
| 87 | + bool updateOnChange = false; |
| 88 | + // Function to call when the model receives an update. |
| 89 | + VoidCallback _updateCallback = () {}; |
| 90 | + void onUpdate() => updateOnChange ? _updateCallback() : () {}; |
| 91 | + |
| 92 | + FlutterFlowModel setOnUpdate({ |
| 93 | + bool updateOnChange = false, |
| 94 | + required VoidCallback onUpdate, |
| 95 | + }) => |
| 96 | + this |
| 97 | + .._updateCallback = onUpdate |
| 98 | + ..updateOnChange = updateOnChange; |
| 99 | + |
| 100 | + // Update the containing page when this model received an update. |
| 101 | + void updatePage(VoidCallback callback) { |
| 102 | + callback(); |
| 103 | + _updateCallback(); |
| 104 | + } |
| 105 | +``` |
| 106 | +
|
| 107 | +## wrapWithModel() |
| 108 | +
|
| 109 | +The `wrapWithModel()` method in FlutterFlow links a model to a widget and its child widgets, allowing them to access and manage state. It wraps the widget with a Provider, making the model available throughout the widget tree. |
0 commit comments