Flutter-整潔架構-MVVM
在本文中,我將簡要分享我在Flutter應用程式中實現清潔架構的方法。
- 星標
- 100
- 分支
- 42
- 最近推送(UTC)
- 2020年6月10日
- 專案狀態
- 未封存
原始 README
以下為英文專案原文快照,最新內容請造訪 GitHub。
Flutter Clean Architecture MVVM sample.
Sample of flutter using Clean Architecture and MVVM.
In this article, we show you how MVVM with Flutter could look like.
We’ll create a functional reactive ViewModel using Darts Stream API.
MVVM
Before we look at any code, we should get a basic understanding of MVVM (Model-View-ViewModel).
How does this look for my Android project?
Module Structure
There are 3 main modules to help separate the code. They are Data, Domain, and Application.
-
Data contains Local Storage, APIs, Data objects (Request/Response object, DB objects), and the repository implementation.
-
Domain contains UseCases, Domain Objects/Models (Pojos/Kotlin Data Classes), and Repository Interfaces
-
Application contains UI, View Objects, Android components, etc. Can be split into separate modules itself if needed. For example, we could have a module called Device handling things like camera, location, etc.
Repository
- Bridge between Data layer and Domain layer
- Connects to data sources and returns mapped data
- Data sources include DB, Api, and RxCache (Cache of data that is constantly emitting updates to subscribers. More on this in another post).
- Always store mapped data. E.g. Store the domain objects in the RxCache.
UseCase
- Responsible for connecting to repository to retrieve necessary data. Can either return a Flowable that will emit each update (from RxCache), or a Single/Completable that finishes after result is retrieved.
- This is where the business logic takes place.
- Returns data downstream.
- Single use.
- Lives in Domain (No Android dependencies. Very testable).
ViewModel
- Organizes data and holds View state.
- Talks to use cases.
- Does not know about the View.
View
- Updates UI
- Knows about the ViewModel
- Observes changes to ViewModel.
Router
- I leave this open ended to suit each projects needs. The main point here is that it is important to consolidate navigation logic to one place. This helps with maintenance and unit testing.
New Feature
- Multi Language
分類
使用的依賴
依賴清單 10 項
- connectivity
^0.3.2 - rxdart
^0.19.0 - http
^0.12.0 - flutter
{"sdk":"flutter"} - flutter_localizations
{"sdk":"flutter"} - cupertino_icons
^0.1.2 - camera
— - path_provider
— - path
— - flutter_test開發依賴
{"sdk":"flutter"}
所在榜單
如何選擇適合專案的儲存庫?
先依使用情境篩選,再比較專案活躍情況,最後到原始儲存庫確認使用要求。
- 透過儲存庫名稱、作者或描述搜尋,再依技術分類縮小範圍。
- 綜合星標、分支與最近推送比較專案;星標反映社群關注,不保證品質。
- 使用前在 GitHub 確認 README、授權條款與 Issues。
來源與維護
“A repository is the most basic element of GitHub.”
GitHub Docs — About repositories ↗