flutter_woocommerce 0.0.3

  • Readme
  • Changelog
  • Example
  • Installing
  • new67

flutter_woocommerce #

Let's integrate woo api with flutter easily.

Getting Started #

Import

import 'package:flutter_woocommerce/flutter_woocommerce.dart';
import 'package:flutter_woocommerce/flutter_woocommerce_utils.dart';

Create instance

FlutterWoocommerce flutterWoocommerce = FlutterWoocommerce(
      url: <url>,
      consumerKey: <your_consumerKey>,
      consumerSecret: <your_consumerSecret>,
  );

Basic CRUD Requests #

Create #

Create data using POST request. Use postReq( String endPoint, Map data ) method. It will return Json Object if no error. Then assign it with corresponding Object. And will return WooError if there is error.

Example of creating a customer using POST Request

  createCustomer() async {
    Customer newCustomer = Customer(
        firstName: "Ma Tin",
        lastName: "Shwe",
        userName: "matinshwe",
        email: "matinshwe@someone.com");
    newCustomer.password = "12345";
    var result =
        await flutterWoocommerce.postReq('customers', newCustomer.toMap());
    if (result is! WooError) {
      Customer createdCustomer = Customer.fromJSON(result);
      print(createdCustomer.userName + " is created successfully!");
    } else {
      WooError err = result;
      print(err.message);
    }
  }

Read #

Read data using GET request. Use getReq( String endpoint ) method. It will return Json Object if no error. Assign it with corresponding Object. And will return WooError if there is error.

But sometimes you may need to use POST to read data according to condition.

Example of reading list of customers using GET Request

  fetchAllOrders() async {
    List<Order> orderList = [];
    var result = await flutterWoocommerce.getReq('orders');
    if (result is !WooError) {
      List<dynamic> resultList = result;
      resultList.forEach((order) {
        orderList.add(Order.fromJSON(order));
      });
    }else{
      WooError err = result;
      print(err.message);
    }
    print(orderList.length);
  }

Update #

Update data using PUT request. Use putReq( String endPoint, Map data ) method. It will return Json Object if no error. Assign it with corresponding Object. And will return WooError if there is error.

Example of updating a product using PUT Request

  updateProduct() async{
    ProductItem productItem = ProductItem(regularPrice: "USD 500");
    var result =
        await flutterWoocommerce.putReq('products/57', productItem.toMap());
    if (result is! WooError) {
      ProductItem createdCustomer = ProductItem.fromJSON(result);
      print(createdCustomer.name + " is updated successfully!");
    } else {
      WooError err = result;
      print(err.message);
    }
  }

Delete #

Delete data using DELETE request. Use deleteReq( String endPoint) method. It will return Json Object if no error. Assign it with corresponding Object. And will return WooError if there is error.

Example of deleting an order using DELETE Request

  deleteOrder() async {
    var result = await flutterWoocommerce.deleteReq('orders/27');
    if (result is! WooError) {
      Order order = Order.fromJSON(result);
      print(order.id.toString() + " is deleted successfully!");
    } else {
      WooError err = result;
      print(err.message);
    }
  }

Authentication #

Intented for customer and admin login. You must install JWT_auth plugin first in your wordpress site.

Use customerLogin( User user ) method to authenticate. It will return WooAuthedUser object if exist.

For Example:

  loginCutomer() async{
    var result = await flutterWoocommerce.customerLogin(User(username: 'username', password: "12345"));
    if(result is! WooError){
      WooAuthedUser wooAuthedUser = result;
      print(wooAuthedUser.displayName);
    }else{
      WooError err = result;
      print(err.message);
    }
  }

Bonus Methods #

There are some additional methods to save your time.

customerSignUp(User user)           => Customer
getCustomers()                      => List<Customer>
updateCustomer(Customer customer)   => Customer
getCustomerByEmail(String email)    => Customer

Objects #

Ready to use util objects. Unchecked items are not completed yet.

  • [x] Coupon
  • [x] Customer
  • [x] Billing
  • [x] Shipping
  • [x] Order
  • [x] LineItem
  • [x] Taxes
  • [x] TaxLine
  • [x] Refund(Order)
  • [x] ShippingLine
  • [x] FeeLine
  • [x] CouponLine
  • [ ] ProductItem
  • [ ] ProductVar
  • [ ] ProductAttr
  • [ ] ProductAttrTerms
  • [ ] ProductCateg
  • [ ] ProductShipClasses
  • [ ] ProductTags
  • [ ] ProductRev
  • [ ] TaxRates
  • [ ] TaxClasses
  • [ ] Settings
  • [ ] SettingsOpt
  • [ ] PaymentGatways
  • [ ] ShippingZones
  • [ ] ShippingZonesMtds
  • [ ] ShippingMtds
  • [ ] SystemStatus
  • [ ] SystemStatTools
  • [ ] data

[0.0.3] - 2020-06-11 #

  • fixed security issue

[0.0.2] - 2020-06-10 #

  • stable version

[0.0.2-dev.0] - 2020-06-10 #

  • Combined all model files to one file named util.

[0.0.1] - 2020-06-4 #

  • Documentation added.

[0.0.1-snowflake.1] - 2020-06-4 #

  • Initial Release.

[0.0.1-snowflake.0] - 2020-06-4 #

  • Initial Release.

example/lib/main.dart

import 'package:example/config.dart';
import 'package:flutter/material.dart';
import 'package:flutter_woocommerce/flutter_woocommerce.dart';
import 'package:flutter_woocommerce/flutter_woocommerce_utils.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHome(),
    );
  }
}

class MyHome extends StatefulWidget {
  @override
  _MyHomeState createState() => _MyHomeState();
}

class _MyHomeState extends State<MyHome> {
  bool _showLoading = false;

  FlutterWoocommerce flutterWoocommerce = FlutterWoocommerce(
      url: ServerCrends.apiUrl,
      consumerKey: ServerCrends.consumerKey,
      consumerSecret: ServerCrends.consumerSecret);

  createCustomer() async {
    _togggleLoading();
    Customer newCustomer = Customer(
      firstName: "Ma Tin",
      lastName: "Shwe",
      userName: "matinshwe",
    );
    newCustomer.setEmail = "matinshwe@someone.com";
    newCustomer.setPassword = "12345";
    var result =
        await flutterWoocommerce.postReq('customers', newCustomer.toMap());
    _togggleLoading();
    if (result is! WooError) {
      Customer createdCustomer = Customer.fromJSON(result);
      print(createdCustomer.userName + " is created successfully!");
    } else {
      WooError err = result;
      print(err.message);
    }
  }

  fetchAllOrders() async {
    _togggleLoading();
    List<Order> orderList = [];
    var result = await flutterWoocommerce.getReq('orders');
    _togggleLoading();
    if (result is! WooError) {
      List<dynamic> resultList = result;
      resultList.forEach((order) {
        orderList.add(Order.fromJSON(order));
      });

      print(orderList.first.createdVia);
    } else {
      WooError err = result;
      print(err.message);
    }
    print(orderList.length);
  }

  updateProduct() async {
    _togggleLoading();
    ProductItem productItem = ProductItem(regularPrice: "USD 500");
    var result =
        await flutterWoocommerce.putReq('products/57', productItem.toMap());
    _togggleLoading();
    if (result is! WooError) {
      ProductItem createdCustomer = ProductItem.fromJSON(result);
      print(createdCustomer.name + " is updated successfully!");
    } else {
      WooError err = result;
      print(err.message);
    }
  }

  deleteOrder() async {
    _togggleLoading();
    var result = await flutterWoocommerce.deleteReq('orders/27');
    _togggleLoading();
    if (result is! WooError) {
      Order order = Order.fromJSON(result);
      print(order.id.toString() + " is updated successfully!");
    } else {
      WooError err = result;
      print(err.message);
    }
  }

  loginCutomer() async {
    _togggleLoading();
    var result = await flutterWoocommerce
        .customerLogin(User(username: 'matinshwe', password: "12345"));
    _togggleLoading();
    if (result is! WooError) {
      WooAuthedUser wooAuthedUser = result;
      print(wooAuthedUser.displayName);
    } else {
      WooError err = result;
      print(err.message);
    }
  }

  _togggleLoading() {
    if (_showLoading) {
      _showLoading = false;
    } else {
      _showLoading = true;
    }
    setState(() {});
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Demo Flutter Woocommerce'),
        centerTitle: true,
      ),
      body: Container(
        width: double.infinity,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            //There are various way to integrate. This is just example
            Text(
              'See result in console',
              style: TextStyle(color: Colors.black26),
            ),
            SizedBox(
              height: 10,
            ),
            FlatButton(
              padding: EdgeInsets.all(15),
              splashColor: Colors.blue,
              textColor: Colors.blue,
              shape:
                  BeveledRectangleBorder(side: BorderSide(color: Colors.blue)),
              child: _showLoading
                  ? SizedBox(
                      child: CircularProgressIndicator(
                        strokeWidth: 1,
                      ),
                      width: 20,
                      height: 20,
                    )
                  : Text(
                      'Run',
                      style: TextStyle(fontSize: 18),
                    ),
              onPressed: _showLoading
                  ? null
                  : () {
                      fetchAllOrders();
                    },
            ),
          ],
        ),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  flutter_woocommerce: ^0.0.3

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:flutter_woocommerce/flutter_woocommerce.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
38
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]
67
Learn more about scoring.

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

  • Dart: 2.8.4
  • pana: 0.13.13
  • Flutter: 1.17.5

Analysis suggestions

Package not compatible with SDK dart

because of import path [flutter_woocommerce] that is in a package requiring null.

Package not compatible with runtime flutter-web on web

Because of the import of dart:io via the import chain package:flutter_woocommerce/flutter_woocommerce.dartdart:io

Maintenance suggestions

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.7.0 <3.0.0
flutter 0.0.0
http ^0.12.1 0.12.1
Transitive dependencies
charcode 1.1.3
collection 1.14.12 1.14.13
http_parser 3.1.4
meta 1.1.8
path 1.7.0
pedantic 1.9.0 1.9.1
sky_engine 0.0.99
source_span 1.7.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6 1.2.0
vector_math 2.0.8
Dev dependencies
flutter_test