livedatax 1.0.0
livedatax: ^1.0.0 copied to clipboard
LiveDataX is a Dart package that brings the original LiveData, MutableLiveData and ViewModel classes from native Android code to Dart. It helps implement the MVVM pattern in Dart applications, followi [...]
LiveDataX #
LiveDataX is a Dart package that brings the original LiveData and MutableLiveData classes from native Android code to Dart. This package is designed to help you implement the MVVM (Model-View-ViewModel) pattern in your Dart applications, following Google's clean architecture principles.
Features #
- LiveData and MutableLiveData classes for reactive data handling.
- Repository pattern for data management.
- ViewModel class to manage UI-related data in a lifecycle-conscious way.
Getting Started #
To start using LiveDataX, add it to your pubspec.yaml
file:
dependencies:
livedatax: ^1.0.0
Then, import it into your Dart project:
import 'package:livedatax/livedatax.dart';
Usage #
Here is a basic example of how to use LiveDataX in your project:
import 'package:livedatax/models/live_data.dart';
import 'package:livedatax/models/mutable_live_data.dart';
import 'package:livedatax/models/view_model.dart';
import '../model/example_model.dart';
import '../repo/example_repository.dart';
class ExampleViewModel extends ViewModel {
ExampleViewModel(dynamic value) : super(value);
ExampleRepository _repository = ExampleRepository();
final MutableLiveData<String> _liveData = MutableLiveData<String>();
late LiveData<String> liveData = _liveData;
@override
void init() {
var newModel = ExampleModel(100, 'Test-init');
_repository.set(newModel);
_liveData.setValue(newModel.value);
}
@override
void dispose() {
_liveData.dispose();
}
void getIntData() {
var newModel = ExampleModel(100, 'Test-fetch');
_repository.set(newModel);
_liveData.postValue(newModel.value);
}
void delete() {
var existingModel = ExampleModel(100, "Test-done");
bool isDeleted = _repository.delete(existingModel);
if (isDeleted) {
_liveData.setValue('Deleted: ${existingModel.value}');
} else {
_liveData.setValue('Delete failed');
}
}
}
import 'package:livedatax/models/repository.dart';
import '../model/example_model.dart';
class ExampleRepository extends Repository<ExampleModel> {
@override
bool delete(ExampleModel data) {
if (allData.value != null && allData.value!.containsKey(data.id)) {
allData.value!.remove(data.id);
return true;
}
return false;
}
@override
ExampleModel? get(ExampleModel? data) {
if (data != null && allData.value != null) {
return allData.value![data.id];
}
return null;
}
@override
void set(ExampleModel? data) {
if (data != null && data.id > 0) {
if (allData.value == null) {
var newMap = <int, ExampleModel?>{};
newMap[data.id] = data;
allData.value = newMap;
} else {
allData.value![data.id] = data;
}
} else {
throw UnimplementedError();
}
}
@override
bool update(ExampleModel data) {
if (allData.value != null && allData.value!.containsKey(data.id)) {
allData.value![data.id] = data;
return true;
}
return false;
}
}
Additional Information #
For more information, visit the GitHub repository to browse through the code. You can also contribute to the package, file issues, and expect responses from the package authors.
License #
This project is licensed under the MIT License. See the LICENSE file for details.