How to pass data dynamically to a Widget class inside Model

Issue

I have a model like this:

class MyModel{
  Widget widget;
  MyModel(this.widget);
}

var myPage = MyModel(HomeWidget());

is there a way to pass data to HomeWidget() using model when i use it? like this?

myPage.widget("some data")

or any way to pass data to widget dynamically?

Solution

u can try callback like way

class MyModel{
  Function(String data) widget;
  MyModel(this.widget);
}

var myPage = MyModel((String data) => HomeWidget(data));
myPage.widget("some data");

Answered By – Husen

Answer Checked By – Clifford M. (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *