live_data 0.0.1

简介 #

一个类似Android LiveData的框架,提供MVVM模式的数据和生命周期绑定。

LiveData采用了观察者模式设计,其中LiveData是被观察者,当数据发生变化时会通知观察者进行数据更新,从而确保数据和界面的实时性。

LiveData能够感知组件的生命周期,所以可以通过设置LiveData组件的生命周期状态,来触发数据更新,从而更新UI。

使用 #

1、创建viewmodel,并创建livedata。

viewmodel内置了livedata的缓存方法,方便多个观察者能观察到同一个数据来源,

class HomeViewModel extends ViewModel {
  MutableLiveData<List<CoolingModel>> mutableLiveData;

  MutableLiveData<List<CoolingModel>> getCoolingModel() {
    if (take("mutableLiveData") == null) {
      mutableLiveData = MutableLiveData<List<CoolingModel>>();
      putLiveData("mutableLiveData", mutableLiveData);
    } else {
      mutableLiveData = take("mutableLiveData");
    }
    return mutableLiveData;
  }
}

2、让你的组件继承LifecycleOwner,给Livedata提供生命周期来源

LifecycleOwner默认提供了STARTED和DESTROYED两个生命周期

class HomeCooling extends StatefulWidget with LifecycleOwner {

}

你也可以通过LifecycleState来定义你自己的生命周期

LifecycleState state = LifecycleState(state:"stop");

3、让你的组件继承LiveDataObserver,并通过泛型确认viewmodel提供的数据的类型。 LiveData会在数据变化后通过 onValueChange方法把改变后的数据通知给组件

class HomeCoolingState extends State<HomeCooling>
    with LiveDataObserver<List<CoolingModel>> {
  List<CoolingModel> models = new List();
  
  @override
  void onValueChange(List<CoolingModel> value) {
    models = value;
    setState(() {});
  }
}

4、向viewmodel中添加observer

homeModel = HomeViewModel().getCoolingModel();
homeModel.addObserver(this, homeCoolingState);

5、MutableLiveData是LiveData的默认实现,提供了setValue方法,通过调用setvalue方法就可以改变数据并通知所有的观察者。 也可以通过自定义LiveData来实现更多功能

void getValue() async {
    List<CoolingModel> list = await CoolingModel.getModels();
    homeModel.setValue(list);
  }

[0.0.1] - TODO: Add release date.

  • TODO: Describe initial release.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  live_data: ^0.0.1

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:live_data/live_data.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
60
Overall:
Weighted score of the above. [more]
42
Learn more about scoring.

We analyzed this package on Jul 22, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.4.0
  • pana: 0.12.19
  • Flutter: 1.7.8+hotfix.3

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

Fix lib/lifecycle.dart. (-1 points)

Analysis of lib/lifecycle.dart reported 2 hints:

line 5 col 25: Name non-constant identifiers using lowerCamelCase.

line 8 col 25: Name non-constant identifiers using lowerCamelCase.

Maintenance suggestions

The package description is too short. (-20 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package.

Common filename patterns include main.dart, example.dart, and live_data.dart. Packages with multiple examples should provide example/README.md.

For more information see the pub package layout conventions.

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11
meta 1.1.6 1.1.7
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test

Admin