mc 0.0.2+2 copy "mc: ^0.0.2+2" to clipboard
mc: ^0.0.2+2 copied to clipboard

State management & request package, Model,View,Controller,Request MVCR.

mc #

State management and request package, Model,View,Controller,Request MVCR. This package mark as discontinued you will found it with new name MVCRocket

Author: Mohammed CHAHBOUN #

Pub License: MIT

Getting Started #

In your flutter project, add the dependency to your pubspec.yaml

dependencies:
  ...
  mc: ^0.0.2+3

Usage #

Simple case use McMV & McValue #

its very simple

class McMiniViewExample extends StatelessWidget {
  // use mini for convert value to McValue
  final McValue<String> myStringValue = "My Value".mini;
  final McValue<int> myIntValue = 2021.mini;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        //use your value in McMV and if value changed will rebuild widget for show your new value
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            // use value for every widget
            McMV(myStringValue, () => Text(myStringValue.v)),
            McMV(myStringValue, () => Text(myIntValue.v.toString())),
            const SizedBox(
              height: 25.0,
            ),
            // merge multi values in one widget
            McMV(McValue.merge([myStringValue, myIntValue]), () {
              return Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: [
                  Text(myStringValue.v),
                  Text(myIntValue.v.toString())
                ],
              );
            })
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        backgroundColor: Theme.of(context).primaryColor,
        onPressed: () {
          // change value
          myStringValue.v = "Value Changed";
          myIntValue.v = 2022;
        },
        tooltip: 'change Value',
        child: Icon(Icons.change_circle),
      ),
    );
  }
}

Complex case (state management & request) #

firstly you need to create your McModel from your json data by this Link you get something like this:

import 'package:mc/mc.dart';

class Post extends McModel<Post> {
  List<Post> multi;
  int userId;
  int id;
  String title;
  String body;

  final String userIdStr = 'userId';
  final String idStr = 'id';
  final String titleStr = 'title';
  final String bodyStr = 'body';

  Post({
    this.userId,
    this.id,
    this.title,
    this.body,
  });

  fromJson(Map<String, dynamic> json) {
    userId = json['userId'] ?? userId;
    id = json['id'] ?? id;
    title = json['title'] ?? title;
    body = json['body'] ?? body;
    return super.fromJson(json);
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['userId'] = this.userId;
    data['id'] = this.id;
    data['title'] = this.title;
    data['body'] = this.body;

    return data;
  }

  void setMulti(List data) {
    List listOfpost = data.map((e) {
      Post post = Post();
      post.fromJson(e);
      return post;
    }).toList();
    multi = listOfpost;
  }
}

Now second step create your McRequest in constructor or initState of first widget and pass url & headers

class MyApp extends StatelessWidget {
  MyApp() {
    const String baseUrl = 'https://jsonplaceholder.typicode.com';
    // create request object
    McRequest request = McRequest(url: baseUrl);
    // save it, for use it from any screen by key
    mc.add('request', request);    
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      ...
    );
  }
}...

Next step its build [McView] Widget & pass your [McModel] in [model] & [McRequest] method in [call] parameter


class PostExample extends StatelessWidget {
  // Save your model to use on another screen
  // readOnly means if you close and open this screen you will use same data without update it from Api
  // [mc] is instance of Mccontroller injected in Object by extension for use it easily anywhere
  final Post post = McController().add<Post>('posts', Post(), readOnly: true);
  // get request by key
  final McRequest request = McController().get<McRequest>("request");
  PostExample({this.title});
  final String title;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(
            "Refresh Posts with swip to down or from here =>",
            style: TextStyle(fontSize: 11.0),
          ),
          actions: [
            IconButton(
                icon: Icon(Icons.data_usage),
                // Refresh Data from Api
                onPressed: () => refresh())
          ],
        ),
        body: Container(
          height: MediaQuery.of(context).size.height,
          width: MediaQuery.of(context).size.width,
          child: RefreshIndicator(
              onRefresh: () {
                return refresh();
              },
              child: McView(
                // call api method
                call: () => request.getObjData("posqts", post, multi: true),
                // your model generated
                model: post,
                // handle errors
                onError: (McException exception,Function() reload) {
                  return Center(
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Text(exception.exception),
                        Text(exception.response),
                        TextButton(onPressed: reload, child: Text("retry"))
                      ],
                    ),
                  );
                },
                // call api if model is empty & you can choose another ways like default way asFuture(call once) & asStream (call every //[secondsOfStream] seconds)
                callType: CallType.callIfModelEmpty,
                // or
                // callType: CallType.callAsStream,
                // secondsOfStream: 1,
                // customized your loading (default widget is CircularProgressIndicator)
                loader:CustomLoading(),
                builder: (context) {
                  return Container(
                    height: MediaQuery.of(context).size.height * 0.852,
                    child: ListView.builder(
                      itemCount: post.multi.length,
                      itemBuilder: (BuildContext context, int index) {
                        // your data saved in multi list as Post model
                        Post currentPost = post.multi[index];
                        return ListTile(
                            leading: Text(post.id.toString()),
                            title: Text(post.title),
                            onTap: () => Navigator.of(context).push(
                                  MaterialPageRoute(
                                      builder: (BuildContext context) {
                                    return Details(index);
                                  }),
                                ));
                      },
                    ),
                  );
                },
              )),
        ));
  }

  Future<dynamic> refresh() {
    // use http method you want (get,post,put) + ObjData if you used model in McView and you can use JsonData for get data directly from api
    return request.getObjData(
    // endpoint
    "posts",
    // your model
    post, 
    // if you received data as List multi will be true & if data as map you not should to define multi its false as default
    multi: true,
    // parameters for send it with request
    params:{"key":"value"},
    // inspect method for determine exact json use for generate your model in first step
    // if your api send data directly without any supplement values you not should define it
    inspect:(data)=>data["response"]
    );
  }
}

& last item its McController for save your model or any value and get it anywhere by key

// inside of object use mc extension 
McController().add("key",value,readOnly:true); // you can't edit it if readonly true
// or
// [add] return value
mc.add<Type>("key",value);
// [get] return value
mc.get<Type>("key");
// [remove]
mc.remove("key");
// remove with condition
mc.removeWhere((key,value)=>key.contains("ke"));

Graphic tutorial #

JPG explain graphic

More examples #

License #

MIT License

Copyright (c) 2021 Mohammed CHAHBOUN

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
20
likes
110
pub points
11%
popularity

Publisher

unverified uploader

State management & request package, Model,View,Controller,Request MVCR.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

flutter, http

More

Packages that depend on mc