Skip to content

MVC and MVVM

Explain MVC and MVVM in flutter using provider

Section titled “Explain MVC and MVVM in flutter using provider”

Sure, I’d be happy to explain MVC (Model-View-Controller) and MVVM (Model-View-ViewModel) using Flutter and Provider.

MVC stands for Model-View-Controller. It is a design pattern which separates an application into three interconnected components:

Model: Represents the data and business logic of the application.
View: Represents the user interface and is responsible for displaying the data provided by the Model.
Controller: Acts as an intermediary between the Model and the View. It processes all the business logic and incoming requests, manipulates data using the Model, and interacts with the View to render the final output.
MVVM stands for Model-View-ViewModel. It also separates an application into three interconnected components:

Model: Same as in MVC, it represents the data and business logic of the application.
View: Again, it represents the user interface and is responsible for displaying the data.
ViewModel: Acts as a link between the Model and the View. It’s responsible for transforming the data from the Model and making it user-friendly for the View. It’s also responsible for handling any commands from the View and executing the appropriate action on the data Model.
Provider is a state management technique in Flutter, which can be used in both MVC and MVVM patterns. Provider helps in managing and propagating state changes across the widgets in the Flutter application.

Here are some simple examples of both patterns in Flutter using Provider:

MVC in Flutter with Provider:
Model:

class CounterModel {
int \_counter \= 0;
int get counter \=\> \_counter;
void incrementCounter() {
\_counter++;
}
}

View:

class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('MVC Example')),
body: Center(
child: Text('You have pushed the button this many times: ${Provider.of\<CounterModel\>(context).counter}'),
),
floatingActionButton: FloatingActionButton(
onPressed: Provider.of\<CounterModel\>(context, listen: false).incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
**Controller**: In MVC, Flutter itself acts as a Controller as it manages the communication between Model and View.
**MVVM in Flutter with Provider:**
**Model:**
class CounterModel {
int \_counter \= 0;
int get counter \=\> \_counter;
void incrementCounter() {
\_counter++;
}
}
**View:**
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('MVVM Example')),
body: Center(
child: Text('You have pushed the button this many times: ${Provider.of\<CounterViewModel\>(context).counter}'),
),
floatingActionButton: FloatingActionButton(
onPressed: Provider.of\<CounterViewModel\>(context, listen: false).incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}

ViewModel:

class CounterViewModel extends ChangeNotifier {
final CounterModel \_counterModel \= CounterModel();
int get counter \=\> \_counterModel.counter;
void incrementCounter() {
\_counterModel.incrementCounter();
notifyListeners();
}
}

The main difference between MVC and MVVM with Provider in Flutter is that in MVC, Flutter itself acts as the Controller whereas in MVVM, ViewModel acts as the Controller and also has the responsibility to notify the View for any changes in the Model.