0

I am trying to migrate from Provider to Riverpod my Flutter code, that is at the moment written in some parts like:

class SomeViewOutput extends StatelessWidget {
  const PayoutFormOutput({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final Widget someWidget = Consumer6<A, B, C, D, E, F>(
        builder: (context, a, b, c, d, e, f, _) {
          var total = a.value + b.value + c.value + d.value + e.value + f.value;
          return ListTile(
              dense: true,
              title: Text("Total", style: style),
              trailing: Text(total, style: style));
        });
    ....do something else
  }

All my notifier classes are like this:

import 'package:flutter/foundation.dart';

class MyState with ChangeNotifier {
  String field;
  double value;

  MyState({this.value = 0.0, this.field = 'No'});

  void set(double value) {
    this.value = value;
    notifyListeners();
  }
  
  void select(String field) {
    this.field = field;
  }
}


class A extends MyState {}
class B extends MyState {}
class C extends MyState {}
class D extends MyState {}
class E extends MyState {}
class F extends MyState {}

I am new to Riverpod and I am trying to figure out a way to convert that. From the documentation it seems to me:

final a = ChangeNotifierProvider<MyState>((ref) { return MyState(); });
final b = ChangeNotifierProvider<MyState>((ref) { return MyState(); });
final c = ChangeNotifierProvider<MyState>((ref) { return MyState(); });
final d = ChangeNotifierProvider<MyState>((ref) { return MyState(); });
final e = ChangeNotifierProvider<MyState>((ref) { return MyState(); });
final f = ChangeNotifierProvider<MyState>((ref) { return MyState(); });

class MyConsumer6 extends ConsumerWidget {
  const MyConsumer6({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    double aValue = ref.watch(a).value;
    double bValue = ref.watch(b).value;
    double cValue = ref.watch(c).value;
    double dValue = ref.watch(d).value;
    double eValue = ref.watch(e).value;
    double fValue = ref.watch(f).value;

    var total = aValue + bValue + cValue + dValue + eValue + fValue;
    return ListTile(
       dense: true,
       title: Text("Total", style: style),
       trailing: Text(total, style: style));
  }
}

is that the right way? Also, what would change if I move from ChangeNotifierProvider to StateNotifierProvider?

Randomize
  • 8,651
  • 18
  • 78
  • 133
  • Seems ok to me, are you trying to get mutable state? If yes you can switch to stateProvider, – Md. Yeasin Sheikh Jul 24 '22 at 10:56
  • yes I was just thinking about taking the state out of the ChangeNotifier and then should be an easy switch to StateNotifier – Randomize Jul 24 '22 at 11:01
  • IMO, i will choose ChangeNotifier when I've multiple variables and like to change specific filed, mostly I use it. But when i have single model class I go for StateProvider. Single one of it ok, also providers can communicate between them using ref, I think your answer depend on coding style. For this case I will create a model class and use StateProvider, Or I will use StateNotifierProvider) – Md. Yeasin Sheikh Jul 24 '22 at 12:04

0 Answers0