mvvm_provider 1.0.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 53

flutter_mvvm #

Simple and effective architecture MVVM using Provider to approach Flutter with lifecycles.

Installing: #

Add the following to your pubspec.yaml file:

dependencies:
  mvvm_provider: ^1.0.1

Usage: #

Let your small/common widget extends FlutterWidget and your full screen widget extends FlutterView with its own WidgetModel/ViewModel.

Or you can create your own base widget and base view.

Check out example for more information.

[1.0.0] - May 20th, 2020 #

  • Initial release

[1.0.1] - May 29th, 2020 #

  • Update version, update introduction, update example

example/lib/main.dart

import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:mvvm_provider/base/FlutterView.dart';
import 'package:mvvm_provider/base/FlutterViewModel.dart';
import 'package:shared_preferences/shared_preferences.dart';

void main() => runApp(ExampleApp());

class ExampleApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'MVVM Example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: HomeView(),
    );
  }
}

class HomeView extends FlutterView {
  @override
  FlutterViewState<FlutterView, FlutterViewModel> getViewState() {
    return _HomeViewState();
  }
}

class _HomeViewState extends FlutterViewState<HomeView, HomeViewModel> {
  @override
  void onViewModelReady() {
    debugPrint('-------------_HomeViewState onCreate');
    viewModel.getTodoList();
  }

  @override
  void onResume() {
    super.onResume();
    debugPrint('-------------_HomeViewState onResume');
  }

  @override
  void onPause() {
    super.onPause();
    debugPrint('-------------_HomeViewState onPause');
  }

  @override
  Widget getView() {
    return Scaffold(
      backgroundColor: Colors.grey[300],
      appBar: AppBar(
        title: Text('MVVM Example'),
      ),
      body: SafeArea(
        child: Column(
          children: <Widget>[
            Expanded(
                child: viewModel.isLoading
                    ? Center(child: CupertinoActivityIndicator())
                    : viewModel.list.isEmpty
                        ? Center(child: Text('No data'))
                        : ListView.separated(
                            padding: EdgeInsets.all(16),
                            itemBuilder: (_, index) =>
                                _todoItem(viewModel.list[index]),
                            separatorBuilder: (_, __) => SizedBox(height: 16),
                            itemCount: viewModel.list.length)),
            Container(
              color: Colors.grey,
              width: double.infinity,
              height: kToolbarHeight,
              child: Center(
                child: FlatButton(
                    onPressed: () {
                      viewModel.addTodo(Todo(
                          DateTime.now().millisecondsSinceEpoch,
                          'Hi ${DateTime.now().millisecondsSinceEpoch}'));
                    },
                    child: Text('Add Todo')),
              ),
            )
          ],
        ),
      ),
    );
  }

  @override
  HomeViewModel getViewModel() {
    return HomeViewModel();
  }

  Widget _todoItem(Todo item) {
    return Container(
      padding: EdgeInsets.all(16),
      color: Colors.white,
      child: Row(
        children: <Widget>[
          Expanded(child: Text(item.todo)),
          SizedBox(width: 8),
          FlatButton(
              onPressed: () {
                viewModel.deleteTodo(item);
              },
              child: Text('Delete'))
        ],
      ),
    );
  }
}

class HomeViewModel extends FlutterViewModel {
  List<Todo> list = [];

  void getTodoList() async {
    final data = await _getTodos();
    list.clear();
    list.addAll(data);
    isLoading = false;
    notifyListeners();
  }

  void addTodo(Todo todo) async {
    final data = await _getTodos();
    data.add(todo);
    await _setTodos(data);
    getTodoList();
  }

  void deleteTodo(Todo todo) async {
    final data = await _getTodos();
    data.removeWhere((element) => element.id == todo.id);
    await _setTodos(data);
    getTodoList();
  }

  Future<List<Todo>> _getTodos() async {
    final sp = await SharedPreferences.getInstance();
    if (!sp.containsKey('todos') || sp.getString('todos') == null) return [];
    final rawJson = sp.getString('todos');
    final parsedJsonList = jsonDecode(rawJson) as List<dynamic>;
    return parsedJsonList.map((json) => Todo.fromJson(json)).toList();
  }

  Future<List<Todo>> _setTodos(List<Todo> data) async {
    final sp = await SharedPreferences.getInstance();
    if (data == null || data.isEmpty) {
      await sp.setString('todos', null);
      return data;
    }
    final parsedJsonList = data.map((single) => single.toJson()).toList();
    await sp.setString('todos', jsonEncode(parsedJsonList));
    return data;
  }
}

class Todo {
  int id;
  String todo;

  Todo(this.id, this.todo);

  Todo.fromJson(Map<String, dynamic> json)
      : id = json['id'],
        todo = json['todo'];

  Map<String, dynamic> toJson() => {'id': id, 'todo': todo};
}

Use this package as a library

1. Depend on it

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


dependencies:
  mvvm_provider: ^1.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:mvvm_provider/base/FlutterView.dart';
import 'package:mvvm_provider/base/FlutterViewModel.dart';
import 'package:mvvm_provider/base/FlutterWidget.dart';
import 'package:mvvm_provider/base/FlutterWidgetModel.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
10
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
90
Overall:
Weighted score of the above. [more]
53
Learn more about scoring.

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

  • Dart: 2.8.4
  • pana: 0.13.15
  • Flutter: 1.17.5

Analysis suggestions

Package not compatible with SDK dart

Because:

  • mvvm_provider that is a package requiring null.

Maintenance issues and suggestions

Repository URL doesn't exist. (-10 points)

At the time of the analysis the repository field https://github.com/ludovn/mvvm_provider was unreachable.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
provider ^4.1.2 4.3.1
visibility_detector ^0.1.5 0.1.5
Transitive dependencies
collection 1.14.12 1.14.13
meta 1.1.8 1.2.2
nested 0.0.4
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8 2.1.0-nullsafety
Dev dependencies
flutter_test