0

I am try pass string from Model1 to Model2 use ChangeNotifierProxyProvider, but it give error:

No setter named ‘string’ in class ‘Model2’

Model1:

class Model1 extends ChangeNotifier {
...
final String string = 'hello';
}

ChangeNotifierProxyProvider:

ChangeNotifierProxyProvider<Model1, Model2>(
      builder: (_, model1, model2) => model2
        ..string = model1.string,
    ),

Model2:

class Model2 extends ChangeNotifier {

String _string;
String get string => _string;

}

Thanks for help!

FlutterFirebase
  • 2,163
  • 6
  • 28
  • 60

1 Answers1

0

You declared the property as read-only.

It cannot be final, and should instead have both a getter and a setter.

Either change it to:

String _string;
String get string => _string;
set string(String value) => _string = value;

or:

String string;
Rémi Rousselet
  • 256,336
  • 79
  • 519
  • 432
  • If I use `String string;` it throw error: `NoSuchMethodError: The setter 'string=' was called on null. Receiver: null Tried calling: string="hello"` – FlutterFirebase Aug 18 '19 at 10:09