Updated 3 January 2024
Flutter, a well-known open-source UI software development kit developed by Google as of my most recent update in September 2021, is not intrinsically based on the MVVM (Model-View-ViewModel) architecture.
Read more about Flutter app development from mobikul.
The model serves as a single source of truth for database-related queries or real-time retrieve data.
This layer may include code validation, business logic, etc. This layer communicates with ViewModel in real-time or for local data. In response to the ViewModel, data are provided.
1 2 3 4 5 6 |
// model.dart class User { String name; User(this.name); } |
ViewModel serves as a liaison between View and Model, accepting all user events and communicating those requests for data to the Model. When the model has data, it goes back to the viewmodel, which then notifies the view of the information.
A single ViewModel can supply data to several Views because it can be used by multiple views.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
// view_model.dart import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'model.dart'; class EditTextViewModel extends ChangeNotifier { User? _user; EditTextViewModel() { _user = User(''); } String? get name => _user?.name; void setName(String newName) { _user?.name = newName; notifyListeners(); } } |
The view is the area of the screen where the user interacts with the Widgets displayed there. These user events call for a few actions that direct users to the ViewModel, where the rest of the ViewModel completes the task. ViewModel updates View once it has the necessary data.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
// main.dart import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'Model/view_model.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return ChangeNotifierProvider( create: (context) => EditTextViewModel(), child: MaterialApp( debugShowCheckedModeBanner:false, home: EditTextView(), ), ); } } class EditTextView extends StatelessWidget { const EditTextView({super.key}); @override Widget build(BuildContext context) { EditTextViewModel viewModel = Provider.of<EditTextViewModel>(context); return Scaffold( appBar: AppBar( title: Text('EditText MVVM Example'), ), body: Padding( padding: EdgeInsets.all(16.0), child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ TextField( onChanged: viewModel.setName, decoration: const InputDecoration( labelText: 'Enter your name', ), ), SizedBox(height: 16), Text( 'Hello, ${viewModel.name}!', style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold), ), ], ), ), ); } } |
Congratulations!! You have learned about the MVVM in Flutter.
Nowadays, MVVM is widely utilized because it enables an event-driven approach, which is essential because many flutter components operate based on events.
Thanks for reading this blog. You can also check other blogs from here for more knowledge.
Always be ready for learning.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.