Flutter Riverpod: Use StateNotifier for Configuration and Update UI

Flutter Riverpod: Use StateNotifier for Configuration and Update UI

Using Riverpod for app's configuration settings and update the UI accordingly. That updating a setting value not only updates UI that listens to that value. I am using Riverpod with StateNotifier to store my app's configurations / settings. Flutter Riverpod: Use StateNotifier for Configuration and Update UI.

I've been using Riverpod with StateNotifier to store my app's configurations / settings. It's easy and convenient as I can use methods to update the state's values. I can always use StateProvider but then I have updated the state directly, which is not convenient (for me) especially when I have a lot of setting parameters.

Using StateNotifier also works perfectly. Well, until recently.. then I found out that updating a setting value not only updates the UI that listens to that value, but also updates other parts of the UI that don't listen to it, but listen to other setting values in the same state. I hope you get what I mean from the last sentence.

To make it clear, let me show you a very simple demonstration.

  • We'll make 2 switches: Switch A and Switch B.
  • Switch A will build Box A, and should not build Box B.
  • Switch B will build Box B, and *should not *build Box A.

Let's code! You can put all the code below into 1 single dart file.

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

class Settings {
  final bool settingA;
  final bool settingB;

  Settings({
    this.settingA = false,
    this.settingB = false,
  });

  Settings copy({
    bool settingA,
    bool settingB,
  }) =>
      Settings(
        settingA: settingA ?? this.settingA,
        settingB: settingB ?? this.settingB,
      );
}

flutter riverpod android flutter-riverpod statenotifier

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Google's Flutter 1.20 stable announced with new features - Navoki

Google has announced new flutter 1.20 stable with many improvements, and features, enabling flutter for Desktop and Web

Top Android Projects with Source Code

Android projects with source code - Work on real-time android projects. We’ll start project ideas from beginners level and later move to advance projects.

What is Flutter and why you should learn it?

Flutter is an open-source UI toolkit for mobile developers, so they can use it to build native-looking Android and iOS applications from the same code base for both platforms. Flutter is also working to make Flutter apps for Web, PWA (progressive Web-App) and Desktop platform (Windows,macOS,Linux).

Adobe XD plugin for Flutter with CodePen Tutorial

Recently Adobe XD releases a new version of the plugin that you can use to export designs directly into flutter widgets or screens.

Building Dark Mode Theme in Android

Hello World, today we are going to see how we can implement a dark theme or night mode in our android application. This tutorial is going to be very simple and easy to understand. The dark theme is attractive to users and it is comfortable for low light conditions.