Skip to content

Software Development at Program Tom LTD

Place for coding, programming, development and software in general.

Menu
  • Blog
  • PDF Booklets
  • Dev Utils & Content
  • Java Spring Boot Or Web Apps
  • English
    • български
    • English
    • Español
    • Português
    • हिन्दी
    • Русский
    • Deutsch
    • Français
    • Italiano
    • العربية
  • About Us
Menu
To Rebuild or not to rebuild the UI in Flutter

To Rebuild or not to rebuild the UI in Flutter

Posted on February 14, 2025 by Toma Velev

ChangeNotifier in Flutter notifies all listeners, even if the data hasn’t actually changed and UI is getting rebuild. This means every widget that calls Provider.of(context) or uses Consumer will rebuild when notifyListeners() is called, regardless of whether the value they depend on has changed. The situation is similar with BLoCBuilder or BLoCListener – when there are values pushed to the streams they listen to.


How to Optimize Rebuilds?

If you want to reduce unnecessary rebuilds, you can use:

1️⃣ Selector (From Provider Package)

Selector listens only to specific parts of a model, preventing unnecessary rebuilds.

Consumer(
builder: (context, notifier, child) {
return Text("Full rebuild: ${notifier.value}");
},
)

// Optimized: Only rebuilds when `value` changes
Selector<MyNotifier, int>(
selector: (_, notifier) => notifier.value,
builder: (_, value, __) {
return Text("Optimized rebuild: $value");
},
)

2️⃣ Read Instead of Watch

  • context.watch() → Causes rebuilds
  • context.read() → Does not rebuild, just gets the value once
  • context.select<MyNotifier, int>((notifier) => notifier.value) → Only rebuilds when value changes

Example:

final value = context.select<MyNotifier, int>((notifier) => notifier.value);
return Text("Only updates when value changes: $value");

3️⃣ Notify Only When Value Changes

Modify your ChangeNotifier to check if the new value is different before calling notifyListeners():

class MyNotifier extends ChangeNotifier {
int _value = 0;

int get value => _value;

void updateValue(int newValue) {
if (_value != newValue) { // Prevent unnecessary rebuilds
_value = newValue;
notifyListeners();
}
}
}

When using a state management package like rx_bloc you could introduce the distict funtion on the streams – so no duplicate values are passed one after another.


4️⃣ Split Notifiers | Use Selectors

If a single ChangeNotifier has multiple values that different widgets depend on, split them into separate notifiers. This reduces unnecessary rebuilds. In the case of rx_bloc – you could use different streams for the different values. in the case of flutter_bloc – you could use BlocSelector – to only listen on certain fields of the state object.

Using Keys

In Flutter, keys are used to identify widgets and their children. More on how important are keys in flutter https://programtom.com/dev/2023/07/15/how-important-are-keys-in-flutter/. When a widget’s key changes, the entire subtree rooted at that widget is rebuilt. Here are some benefits of using keys in Flutter rebuilds:

  1. Improved Performance: When a widget’s key changes, the entire subtree is rebuilt, which can be faster than rebuilding individual widgets. This is because Flutter’s widget tree is optimized for fast rebuilds of large subtrees.
  2. Better Support for Dynamic Data: Keys allow you to easily rebuild widgets when the underlying data changes, but do not – when the values are the same.
  3. Improved Support for Animations: Keys are necessary for animations that involve rebuilding the widget tree.
  • Jenkins SCP File Upload to Remote Server
  • Essential Programming Books – Principles & Flutter
  • Social Media Platforms 🌍
  • Strategies to prevent review regressions
  • How to set Google Map Theme in a Flutter App

Categories

  • Apps (22)
  • ChatGPT (23)
  • Choosing a Framework (38)
  • Flutter (269)
  • Graphical User Interface (14)
  • Marketing (117)
  • Software Development (286)
  • Spring (45)
  • StartUp (22)
  • Uncategorized (14)
  • Uncategorized (4)
  • Vaadin (15)

Tags

Algorithms (9) crypto (29) flutterdev (39) General (86) Java (7) QR & Bar Codes (3) Software Dev Choices (33) Spring Boot (1) standards (1) Theme (3) User Authentication & Authorization (9) User Experience (10) Utilities (19) WordPress (11)

Product categories

  • All Technologies (84)
    • Flutter Apps (24)
    • GPT (4)
    • Java (38)
    • Native Android (3)
    • PHP (9)
    • Spring (Boot) / Quarkus (35)
    • Utils (15)
    • Vaadin 24+ (27)
    • Vaadin 8 (1)
  • Apps (18)
    • Employees DB (1)
    • Notes (6)
    • Personal Budget (1)
    • Recipes Book (1)
    • Stuff Organizer (1)
    • To-Do (2)
  • PDF Books (3)
  • Source Code Generators (8)

Recent Posts

  • Jenkins SCP File Upload to Remote Server
  • Essential Programming Books – Principles & Flutter
  • Social Media Platforms 🌍
  • Strategies to prevent review regressions
  • How to set Google Map Theme in a Flutter App

Post Categories

  • Apps (22)
  • ChatGPT (23)
  • Choosing a Framework (38)
  • Flutter (269)
  • Graphical User Interface (14)
  • Marketing (117)
  • Software Development (286)
  • Spring (45)
  • StartUp (22)
  • Uncategorized (14)
  • Uncategorized (4)
  • Vaadin (15)