The class StatelessWidget is marked as immutable. However, I am using the scoped model, which means that I avoid StatefulWidget and use the model to alter state in StatelessWidget. This leads to me having non-final fields in StatelessWidget, which doesn't cause errors, because it's just a warning. But I wondered if there is a better way?
Flutter: Mutable fields in stateless widgets
6.7k views Asked by footurist At
2
There are 2 answers
0
UsabilitySpace
On
Here's you question:
Do you think there is a better approach to accomplish what I want while keeping the "short class structure" that helps me keep oversight and being able to trigger rebuilds of those classes from anywhere?
What you're asking for here ↑ seems to be one more app state management approach that should be better than scoped model.
As you know, app state management approaches are a set of techniques that allow you as a developer:
- to bind data with widgets.
Binding data with widgets, in turn, helps you as a developer:
- to rebuild widgets automatically on every change of bound data.
Maybe, for that purpose, you can make use of rxdart:
Here you will find some very helpful list of app state management approaches that can lead you to a better way of app development:
Related Questions in DART
- Flutter + Dart: Editing name of a tab shows up a black screen
- The Binary Version Of its metadata is 1.8.0, expected Version is 1.6.0 build error
- How to make barrier area interactive in flutter modal bottom sheet
- Why do I need to wait to reaccess to Firestore database even though it has already done before?
- I wrote this time displaying FLUTTER app, How can I improve it?
- How Do I Create A Path In The pubspec.yaml File?
- Hi, there is an error happened when I build my flutter app, after I'm installing firebase packages occurs that error
- The copy/paste functionalities don't work only on iOS in the Flutter app
- 'New messages' separator like WhatsApp
- How to implement pagination on the custom dropdown item's in flutter
- How to create a listTile from a class in Flutter based on an collected id from the previous page
- I am getting "binding has not yet been initialized" error when trying to connect firebase with flutter
- Issue with Loading Images in Flutter FragmentShader and GLSL Code
- How to change the return type of memoizer?
- What is the difference between `context.watch` and `context.select` in flutter_bloc?
Related Questions in FLUTTER
- Flutter + Dart: Editing name of a tab shows up a black screen
- The Binary Version Of its metadata is 1.8.0, expected Version is 1.6.0 build error
- Way to get CustomPainter to track face in Camera Flutter MLKit
- flutter Null check error: did not show file and line number
- Creating multiple instances of a class with different initializing values in Flutter
- I want to paste stickers into to my TextField and to show the stickers beside the emojis
- Flutter plugin development android src not opening after opening example
- Module not found when building flutter app for IOS
- How to make barrier area interactive in flutter modal bottom sheet
- Can an RPC result be included in a Supabase select function in Flutter for Data Modeling?
- Why do I need to wait to reaccess to Firestore database even though it has already done before?
- Flutter web app on Windows -how to support mouse drag for horizontal and vertical scrolling as well as using mouse wheel
- I wrote this time displaying FLUTTER app, How can I improve it?
- Appwrite and / or Spring Boot Backend
- Flutter two_dimensional_scrollables Web app Chrome - cannot get horizontal scroll to work?
Related Questions in STATE
- NextJS 13 14, Store server state globally
- My state is undefined despite being setted
- Next.js carousel component loads instagram basic display api perfectly except in Safari browser
- How can I create a javascript animation that recognizes when it hits things, in other words a hit box?
- Flutter - When an element is deleted from the ListView, the state of the element under deletion is reset
- State Transfer from a model using Riverpod flutter
- React components don't re render when the state is changed
- Flutter | Persisting Navigator State Using Hydrated Bloc with the Bloc Design Pattern
- Wait set state finishes to call another function
- keyboard focus with GTK4 Rust does not work as expected and how to get state
- Bind state to context value in React JS
- I have a problem when I run on the web Bad state: databaseFactory not initialized databaseFactory
- How to manage state in a chrome extension in this scenario
- Should I use immutable or mutable state for a JSON tree editor in Flutter?
- React Component doesn't change states until file is resaved, then runs both at same time
Related Questions in IMMUTABILITY
- How can I implement immutable builds while using "rebase and merge" in a github repo?
- Framework for Creating Immutable Identifiers
- Python Frozen Nessted Dataclass, with __call__ = replace
- Why does the immutability of strings in java cause extra runtime for this problem: https://leetcode.com/problems/search-suggestions-system/solution/
- How to make a list (or any other value) immutable: a real constant in Python?
- Handle changes in calculation logic for order system
- Is adding 'readonly' to a DirectInjection service (in Symfony) useful?
- Unexpected `FrozenInstanceError` in unit test when using a frozen exception
- Suffix List of Strings with the number of times they were repeated before in Scala
- How to address mutable parameters like datatime in Powershell?
- Exposing an Immutable and Mutable Record Class C#
- Why does is x and y immutable in JavaScript running in an <IMG> event handler?
- Mutable list in data class being initialized as ImmutableCollections$ListN
- How to declare TypeVar as immutable
- How to resolve mutable borrow followed by immutable borrow within a loop in Rust?
Related Questions in STATELESSWIDGET
- Why is the value of a variable "ButtonCount" declared before the build method not retaining its value in flutter
- How can Error in flutter stateless widget?
- Is using a StatelessWidget without a const constructor better than using a StatefulWidget?
- I made changes in stateless widget structure
- Why is IconButton a stateless widget in Flutter, rather than being a stateful widget?
- Flutter Stateless Widget initialize values by keeping the constructor as "const"
- transter data from statefull widget to another
- Are there any problems with this use of a Provider instead of a StatefulWidget?
- StatelessWidget gets rebuilt even though parameters stay the same; notifyListeners() with ChangeNotifier
- How to fix `Do not use BuildContexts across async gaps` in a StatelessWidget?
- How to use a method in a Stateful Widget from another Widget
- Flutter : DefaultTabController Tabbar Tab Color not changing on selection
- Flutter Bottomsheet reload problem after dismissing keyboard
- draggableScrollableController not attached to a sheet after it has been attached
- Main app widget: setState call -> build() builds all widgets but screen not updated
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Stateless widgets should only have final fields, with no exceptions. Reason: When the parent widget is rebuilt for some reason (screen rotation, animations, scrolling...), the
buildmethod of the parent is called, which causes all widgets to be reconstructed.Classes the extend
StatefulWidgetmust follow the same rule, because those are also reconstructed. Only theState, which can contain mutable fields, is kept during the lifetime of widget in the layout tree.There is no reason to avoid
StatefulWidget. It is a fundamental building block of Flutter.In fact,
ScopedModelDescendantis also a stateful widget. The primary benefit of scoped_model is that you can separate the business logic from the widget layer. It doesn't eliminate the need for stateful widgets.Use stateful widgets for:
ScopedModelwidget). Store theModelinstance in theState.TextEditingController, state of a checkbox)AnimationControllersController(TabController,ScrollController, ...)It is often a good idea to make the "page" widgets (widgets which build a
Scaffold, accessible using theNavigator) stateful. Often these are the hosts for scoped models.