livedatax 1.0.0 copy "livedatax: ^1.0.0" to clipboard
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
copied to clipboard

Then, import it into your Dart project:

    import 'package:livedatax/livedatax.dart';
copied to clipboard

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');
        }
      }
    }
copied to clipboard
    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;
      }
    }
copied to clipboard

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.

1
likes
130
points
31
downloads

Publisher

verified publisher3p-cups.com

Weekly Downloads

2024.09.14 - 2025.03.29

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, following Google's clean architecture principles.

Repository (GitHub)

Documentation

API reference

License

MIT (license)

More

Packages that depend on livedatax