Flutter initiating a provider and changing its value / NoSuchMethodError

Viewed 215

I struggle changing the values of my providers.

They are at the top of my widget tree :

runApp(
    MultiProvider(
      providers: [
        ChangeNotifierProvider(
          create: (context) => User(),
        ),
        ChangeNotifierProvider(
          create: (context) => DisplayModel(),
        ),
      ],
      child: MyApp(),
    ),
  );

Here is the class where I try to build function to change the provider value. DisplayChange() should allow user to set his own values for DisplayModel.

class DisplayModel extends ChangeNotifier {
  String blogId;
  String title;
  String subtitle;
  String desc;
  String author;
  String publisherId;
  String imageUrl;

  DisplayModel(
      {this.blogId,
      this.title,
      this.subtitle,
      this.desc,
      this.author,
      this.publisherId,
      this.imageUrl});

  factory DisplayModel.fromDocument(doc) {
    return DisplayModel(
      blogId: doc['blogId'],
      title: doc['title'],
      subtitle: doc['subtitle'],
      desc: doc['desc'],
      author: doc['author'],
      publisherId: doc['publisherId'],
      imageUrl: doc['imageUrl'],
    );
  }


  DisplayChange(DisplayModel) {
    DisplayModel (DisplayModel);
     notifyListeners();
     return DisplayModel();
  }
}

I call the provider in MyApp() and I want the user to be able to change the instance of DisplayModel with the values of loadebBlog (it's built with the same constructors as DisplayModel) and be redirected to home of the app

 child: GestureDetector(
                      onTap: () {
                        Provider.of<DisplayModel>(
                          context,
                          listen: false,
                        ).DisplayChange(loadedBlog);

                        print ("ok");

                       Navigator.of(this.context).pushReplacementNamed('home');
                      },
                    child: Icon(Icons.ios_share),),

But when the button is tapped, it returns an error

The following NoSuchMethodError was thrown while handling a gesture:
Class 'BlogModel' has no instance method 'call'.
Receiver: Instance of 'BlogModel'
Tried calling: call(Instance of 'BlogModel')

Is the logic good ? What am I missing ?

Thank you for your time.

Best regards, Julien

1 Answers

Just came across this, so I don't know if it's helpful after 4 months but..

This syntax seems weird:

 DisplayChange(DisplayModel) {
    DisplayModel (DisplayModel);
     notifyListeners();
     return DisplayModel();
  }

Should be something like:

 DisplayChange(DisplayModel model) {
     // I'm not sure what this is supposed to do
     // but it's no-op code
     // DisplayModel (DisplayModel);
     notifyListeners();
     return DisplayModel();
  }
Related