Created
April 1, 2020 16:28
-
-
Save JEuler/e563222a926773103458ee67a0b589e8 to your computer and use it in GitHub Desktop.
MobX Flutter store example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
part 'user_store.g.dart'; | |
/// User Store | |
class UserStore = UserStoreBase with _$UserStore; | |
/// User Store | |
abstract class UserStoreBase with Store { | |
final WebService _webService; | |
final StorageService _storageService; | |
/// User | |
@observable | |
Observable<DBUser> user = Observable(null); | |
/// Constructor | |
UserStoreBase(this._webService, this._storageService); | |
/// Load User from WebService | |
@action | |
Future<void> loadUser(int projectId, int id) async { | |
final offlineUser = await _storageService.getUser(projectId, id); | |
if (offlineUser != null) | |
user = Observable(offlineUser); | |
} | |
await _webService.getUser(projectId, id).then((response) { | |
if (response.isSuccessful) { | |
user = ObservableList.of(_convertAndPersistResponse(response.body)); | |
} | |
}); | |
} | |
_convertAndPersistResponse(User user) { | |
_storageService.saveUser(user.dbVersion()); // extension method | |
return dbList; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment