view_model 0.2.1

view_model #

Support MVVM Pattern

Example usages

Installing #


$ cd example/
$ flutter packages pub run build_runner build

For generation ''main.reflectable.dart''

Getting Started #

For help getting started with Flutter, view our online documentation.

For help on editing package code, view the documentation.

0.2.1 #

  • Static provide viewModel

0.2.0 #

  • Global refactor code, use of reflection to create ViewModel

0.1.2 #

  • Added example project
  • Refactor ViewModelProvider
  • Deleting Page and PageState
  • Added method ViewModel.newInheritedWidget

0.1.1 #

  • Initial Release.


import 'dart:async';

import 'package:flutter/material.dart';
import 'package:view_model/view_model.dart';
import 'main.reflectable.dart';

void main() {
  runApp(new MyApp());

class MyApp extends StatelessWidget {

  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
      home: HomePage(),

class HomePage extends StatefulWidget {
  _HomePageState createState() => _HomePageState();

class _HomePageState extends State<HomePage> {
  HomeViewModel viewModel;

  Widget build(BuildContext context) {
    viewModel = ViewModelProvider.provide(HomeViewModel);
//    var factory = MyViewModelFactory(data: 'Test');
//    viewModel = ViewModelProvider().provide(HomeViewModel, factory: factory);
    return Scaffold(
      appBar: AppBar(
        title: Text('ViewModel test'),
      body: Center(
        child: Column(
          children: <Widget>[
              'You have pushed the button this many times:',
              stream: viewModel.clickStream,
              builder: (context, snapshot) =>
                  Text(snapshot.hasData ? '${}' : '0'),
      floatingActionButton: ViewModelInherited(
          viewModel: viewModel,
          child: MyWidget()),

  void dispose() {

class MyWidget extends StatefulWidget {
  _MyWidgetState createState() => _MyWidgetState();

class _MyWidgetState extends State<MyWidget> {
  HomeViewModel viewModel;

  Widget build(BuildContext context) {
    viewModel = ViewModel.of<HomeViewModel>(context);
    return FloatingActionButton(
      onPressed: () {
      tooltip: 'Increment',
      child: new Icon(Icons.add),

class HomeViewModel extends ViewModel {
  StreamController<int> _clickStreamController;
  Stream<int> clickStream;
  String data;

  HomeViewModel({}) {
    _clickStreamController = new StreamController<int>.broadcast();
    var counter = 0;
    clickStream = {
      counter += val;
      return counter;

  void increment() {
    print('======== $data ========');

  void dispose() {
    // implement dispose

//Use the factory if you need to transfer data to the constructor
class MyViewModelFactory extends ViewModelFactory {
  String data;


  T crateViewModel<T extends ViewModel>(Type type) {
    var vm;
    switch (type) {
      case HomeViewModel:
        vm = HomeViewModel(data: data);
    if (vm == null) throw Exception('Not implemented class $type');
    return vm;


Use this package as a library

1. Depend on it

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

  view_model: ^0.2.1

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter pub get

Alternatively, your editor might support pub get or 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:view_model/view_model.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

The package version is not analyzed, because it does not support Dart 2. Until this is resolved, the package will receive a health and maintenance score of 0.

Analysis issues and suggestions

Support Dart 2 in pubspec.yaml.

The SDK constraint in pubspec.yaml doesn't allow the Dart 2.0.0 release. For information about upgrading it to be Dart 2 compatible, please see