flutter
  1. flutter-architecture

Flutter Architecture

Flutter is an open-source mobile application development framework which utilizes the Dart language. The framework is designed to make the building of mobile applications easier and faster by providing easy-to-use widgets, tools, and libraries. To ensure the scalability and maintainability of the application built using Flutter, it’s important to adopt a strong architecture that follows the Separation of Concerns (SoC) principle.

The Importance of Flutter Architecture

Architecture plays a key role in determining the efficiency, maintainability, and scalability of an application. When building an application with Flutter, there are a variety of architectural patterns available. The most popular architecture pattern currently followed in Flutter is the MVVM (Model-View-ViewModel) pattern.

MVVM Architecture

The MVVM architecture pattern provides an easier and faster way of architecting the application. It also separates the presentation logic from the business logic of the application and helps to maintain code reusability. Let's go through the syntax, example, output, explanation, use, important points, and a summary of the MVVM architecture pattern.

Syntax

The MVVM architecture can be divided into three parts:

  • Model: It contains the data that the application will use, the repositories, and the network layers.
  • View: It represents the user interface and the visible elements on the screen.
  • ViewModel: It contains all the business logic of the application. It bridges the gap between the view and the model.

Example

class User {
  final String name;
  final String email;
  final String password;

  User({this.name, this.email, this.password});
}

class UserViewModel extends ChangeNotifier {
  List<User> users = [
    User(name: 'John', email: '[john@example.com](mailto:john@example.com)', password: '1234'),
    User(name: 'Jane', email: '[jane@example.com](mailto:jane@example.com)', password: '5678'),
    User(name: 'Bob', email: '[bob@example.com](mailto:bob@example.com)', password: '9123'),
    User(name: 'Alice', email: '[alice@example.com](mailto:alice@example.com)', password: '4567')
  ];

  addUser(User user) {
    users.add(user);
    notifyListeners();
  }

  deleteUser(User user) {
    users.remove(user);
    notifyListeners();
  }
}

Output

The output of the above example is the creation of a User class and a UserViewModel class that contains the business logic of the application.

Explanation

The User class is the model that contains the data that the application uses. The UserViewModel class contains all the business logic of the application. It has two methods, addUser and deleteUser, which are used to add and delete the user data. The notifyListeners method is called to let the view know that the data has been updated.

Use

The ViewModel class is used to drive the view. It interacts with the Model and provides the data to the View. It contains the business logic of the application. In MVVM architecture, Views should only have knowledge of the ViewModel, and not the Model.

Important Points

  • The View should only have knowledge of the ViewModel.
  • The ViewModel should contain all the business logic of the application.
  • The Model should only have knowledge of itself and not the ViewModel or view.

Summary

In conclusion, the MVVM architecture pattern provides an easier and faster way of architecting the application. It separates the presentation logic from the business logic of the application and helps to maintain code reusability. Remember to follow the separation of concerns principle to make the code scalable, maintainable, and easy to understand.

Published on: