State Management with provider Flutter – P2 — Multi provider

1.Tạo project:

flutter create state_multi_provider_demo

2.Setup provider:

flutter pub add provider

3.Import package provider:

import 'package:provider/provider.dart';

Tạo class MySettings để change background color:


class MySettings extends ChangeNotifier {
  bool _isDark = false;

  get isDark => _isDark;

  void setBrightness(bool value) {
    _isDark = value;
    notifyListeners();
  }
}

Tạo class Counter để tăng count


class Counter extends ChangeNotifier {
  int _myCounter = 0;
  get myValue => _myCounter;

  void add() {
    _myCounter++;
    notifyListeners();
  }
}

Full code: