Skip to content

Instantly share code, notes, and snippets.

@JEuler
Created April 1, 2020 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JEuler/e563222a926773103458ee67a0b589e8 to your computer and use it in GitHub Desktop.
Save JEuler/e563222a926773103458ee67a0b589e8 to your computer and use it in GitHub Desktop.
MobX Flutter store example
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