I am using MVVM architecture and used stacked dependency for this. I want to call a method exist in ViewModel class from View class. In this view class trigger method is Widget build(BuildContext context)
So I am unable to get reference of ViewModel class. Is there any way to achieve this.
For more details I have added my code for Stateless Widget:
class ECRView extends StatelessWidget {
@override
Widget build(BuildContext context) {
// TODO: implement build
SystemChrome.setEnabledSystemUIOverlays(SystemUiOverlay.values);
return ViewModelBuilder<ECRViewModel>.reactive(
onModelReady: (model) {
model.init(context);
},
builder: (context, model, child) => Container(
padding: EdgeInsets.all(AppSize.extraSmall),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Container(
width: screenSize.width,
height: 1.5,
color: Colors.black12,
),
SizedBox(
height: screenSize.height * .02,
),
],
),
),
viewModelBuilder: () => ECRViewModel(),
);
}
//Trigger ECR Model Method
getTriggered(){
//From here I want to call
}
}
This should work on your case, if you want to use the model on widgets
class _MyWidget extends ViewModelWidget<NameViewModel> {
@override
Widget build(BuildContext context, NameViewModelmodel model) {
//can call model
return //some code
}
}
This is not the best option for stacked architecture
class _MyWidget extends StatefulWidget {
final NameViewModelmodel model;
const _MyWidget({Key key, this.model}) : super(key: key);
@override
__MyWidgetState createState() => __MyWidgetState();
}
class __MyWidgetState extends State<_MyWidget> {
@override
Widget build(BuildContext context) {
return Container();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.