mvcprovider 1.0.6

  • Readme
  • Changelog
  • Example
  • Installing
  • 65

mvcprovider #

Create a simple MVC architecture based on the Provider package.

Getting Started #

Everybody need to create a counter module, and of course you need it too.

STEP 1 #

First, define your very complex model :

    class MyCounterModel extends Model {

      int _count = 0;
      set count(int value) {
         _count = value;
        refresh();
       }
      int get count => _count;

    }

STEP 2 #

Then define a controller that will increment the count property with a lot of mathematics operations :

    class MyCounterCtrl extends Controller<MyCounterModel> {
    
      void increment() {
        model.count++;
      }
    
      /*
      // Uncomment if needed

      // You can access to injected providers everywhere in your controller and your view
      // Just by using : get[MyProviderClass]

      // Init is called one time before view's first render
      @override
      Controller init(BuildContext context, MyCounterModel model, Map<Type, dynamic> dependencies) {
        super.init(context, model, dependencies);

        // Do some cool things here that you need to do before
        // using this controller or rendering the view

        return this;
      }

      // OnRefresh is called each time the view is rendered
      @override
      Controller onRefresh(BuildContext) {
        super.onRefresh(context);

        // Do some cool things each time you refresh the view

        return this;
      }
      */
    
    }

For now, you have an amazing method increment that will increment (no ?! really ??) you're model's property count which will call refresh (a method alias for the method notifyListeners inherited from her mother class ChangeNotifier) in order to refresh the view.

STEP 3 #

But to refresh the view. You need a view. Here it is :

    class MyCounterView extends View<MyCounterModel, MyCounterCtrl> {
    
      @override
      Widget build(BuildContext context, [Widget child]) {
        super.build(context);
    
        return Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text("Counter : ${model.count}"),
            RaisedButton(
                child: Text("Increment"),
                onPressed: ctrl.increment,
            )
          ]
        );
      }
    }

It's a bit complicated view. I'll explain : it's a text on top of a button.

Like you can see the text display our model's property count and the button execute our controller's method increment when pressed.

STEP 4 #

In order to make the magic happen you'll need to link these three entities together, I decided that this fusion will be called a Module but you can call this Roberto or anything else if you prefer.

Here is our module :

    class MyCounterModule extends Module<MyCounterModel, MyCounterView, MyCounterCtrl> {
    
      final MyCounterModel model = MyCounterModel();
      final MyCounterView view = MyCounterView();
      final MyCounterCtrl ctrl = MyCounterCtrl();
    
      /*
      // Uncomment if needed

      // You can access to injected providers everywhere in your controller and your view
      // Just by using : get[MyProviderClass]

      // You can declare & inject here all brand new providers that you need
      final List<SingleChildWidget> providers = [
        ChangeNotifierProvider(create: (context) => MyProviderClass1()),
        ChangeNotifierProvider(create: (context) => MyProviderClass2()),
      ];

      // You can inject here all already declared providers that you need
      final Map<Type, dynamic> dependencies = {
        MyProviderClass2: (context) => Provider.of<MyProviderClass1>(context)
        MyProviderClass3: (context) => Provider.of<MyProviderClass2>(context)
        MyProviderClass4: (context) => Provider.of<MyProviderClass3>(context)
      };
      */
    
    }

STEP 5 #

At this point if you're a good developer you should have a module folder named mycounter with 4 files inside :

The module my.counter.dart The model my.counter.model.dart The view my.counter.view.dart The controller my.counter.ctrl.dart

You can also have only one file with all your code inside but it's mean you're a bad developer ;)

To test you newly counter feature in your app just call your widget MyCounterModule() Example in your main.dart :

    void main() => runApp(AmazingSample());
    
    class AmazingSample extends StatelessWidget {
      // This widget is the root of this amazing package sample.
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'MVC Provider counter demo',
          home: MyCounterModule(),
        );
      }
    }

[1.0.0] 03-14-2020 #

First release

[1.0.1] 03-14-2020 #

Fixed dart warnings and added an example folder with an example :)

[1.0.2] 03-14-2020 #

Ran flutter lib/mvcprovider.dart

[1.0.3] 03-14-2020 #

Replaced module's root widget Material by a widget Container

[1.0.4] 03-15-2020 #

Removed View's "StatelessWidget extends" in order to use it as a mixin and so permit to use it with StatefulWidgets if needed

[1.0.5] 06-10-2020 #

Updated sample doc Added getters for model and ctrl properties in the View mixin in order use it while extending from a StatelessWidget without the "immutable class with non final fields" warning

[1.0.6] 06-30-2020 #

Little refacto And added ability to add declare new providers at module level with "providers" property in addition to the "dependencies" property which have been designed to inject already declared providers

example/main.dart

import 'package:flutter/material.dart';

import 'modules/mycounter/my.counter.dart';

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

class AmazingSample extends StatelessWidget {
  // This widget is the root of this amazing package sample.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'MVC Provider counter demo',
      home: MyCounterModule(),
    );
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  mvcprovider: ^1.0.6

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:mvcprovider/mvcprovider.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
30
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
65
Learn more about scoring.

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

  • Dart: 2.8.4
  • pana: 0.13.14
  • Flutter: 1.17.5

Analysis suggestions

Package not compatible with SDK dart

Because:

  • mvcprovider that is a package requiring null.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
flutter 0.0.0
provider ^4.0.4 4.3.1
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