Skip to content

Commit 4780502

Browse files
Merge branch 'Nicuz-master'
2 parents f818591 + 28f882c commit 4780502

File tree

10 files changed

+282
-171
lines changed

10 files changed

+282
-171
lines changed

README.md

Lines changed: 17 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -4,103 +4,26 @@ A dart package to interact with the WooCommerce API. It uses OAuth1.0a behind th
44

55
![Example code and preview](Screenshot.png)
66

7-
## Complete Usage Example
8-
```
9-
import 'dart:async';
10-
11-
import 'package:flutter/material.dart';
12-
import 'package:woocommerce_api/woocommerce_api.dart';
13-
14-
void main() => runApp(new MyApp());
15-
16-
class MyApp extends StatelessWidget {
17-
@override
18-
Widget build(BuildContext context) {
19-
return new MaterialApp(
20-
title: 'Flutter Demo',
21-
theme: new ThemeData(
22-
primarySwatch: Colors.blue,
23-
),
24-
home: new MyHomePage(title: 'WooCommerce API Demo'),
25-
);
26-
}
27-
}
28-
29-
class MyHomePage extends StatefulWidget {
30-
MyHomePage({Key key, this.title}) : super(key: key);
31-
32-
final String title;
33-
34-
@override
35-
_MyHomePageState createState() => new _MyHomePageState();
7+
## Examples
8+
9+
### GET request (Fetch products)
10+
```dart
11+
Future getProducts() async {
12+
// Initialize the API
13+
WooCommerceAPI wooCommerceAPI = WooCommerceAPI(
14+
url: "https://www.yourwebsite.com",
15+
consumerKey: "ck_your_consumer_key",
16+
consumerSecret: "cs_your_consumer_secret");
17+
18+
// Get data using the "products" endpoint
19+
var products = await wooCommerceAPI.getAsync("products");
20+
return products;
3621
}
37-
38-
class _MyHomePageState extends State<MyHomePage> {
39-
40-
List<Widget> products = [];
41-
42-
Future getProducts() async {
43-
44-
/// Initialize the API
45-
WooCommerceAPI wc_api = new WooCommerceAPI(
46-
"https://www.yourwebsite.com",
47-
"ck_your_consumer_key",
48-
"cs_your_consumer_secret"
49-
);
50-
51-
/// Get data using the endpoint
52-
var p = await wc_api.getAsync("products");
53-
return p;
54-
}
55-
56-
@override
57-
void initState() {
58-
super.initState();
59-
}
60-
61-
@override
62-
Widget build(BuildContext context) {
63-
return new Scaffold(
64-
appBar: new AppBar(
65-
title: new Text(widget.title),
66-
),
67-
body: FutureBuilder(
68-
future: getProducts(),
69-
builder: (_, s){
70-
71-
if(s.data == null){
72-
return Container(
73-
child: Center(
74-
child: Text("Loading..."),
75-
),
76-
);
77-
}
78-
79-
return ListView.builder(
80-
itemCount: s.data.length,
81-
itemBuilder: (_, index){
82-
83-
/// create a list of products
84-
return ListTile(
85-
leading: CircleAvatar(
86-
child: Image.network(s.data[index]["images"][0]["src"]),
87-
),
88-
title: Text(s.data[index]["name"]),
89-
subtitle: Text("Buy now for \$ " + s.data[index]["price"]),
90-
);
91-
92-
}
93-
);
94-
},
95-
),
96-
);
97-
}
98-
}
99-
10022
```
23+
You can find a full example [here](example/fetch_products.dart)
10124

102-
### Example of making a POST (Create a customer)
103-
```
25+
### POST request (Create a customer)
26+
```dart
10427
Future createCustomer() async {
10528
try {
10629
var response = await wooCommerceAPI.postAsync(

android/local.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
sdk.dir=/Users/samarthagarwal/Library/Android/sdk
2-
flutter.sdk=/Users/samarthagarwal/flutter
1+
sdk.dir=/Users/Nico/Library/Android/sdk
2+
flutter.sdk=/Users/Nico/flutter
33
flutter.versionName=0.0.7

example/fetch_products.dart

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:woocommerce_api/woocommerce_api.dart';
3+
4+
void main() => runApp(MyApp());
5+
6+
class MyApp extends StatelessWidget {
7+
@override
8+
Widget build(BuildContext context) {
9+
return MaterialApp(
10+
title: 'Flutter Demo',
11+
theme: ThemeData(
12+
primarySwatch: Colors.blue,
13+
),
14+
home: MyHomePage(title: 'WooCommerce API Demo'),
15+
);
16+
}
17+
}
18+
19+
class MyHomePage extends StatefulWidget {
20+
MyHomePage({Key key, this.title}) : super(key: key);
21+
22+
final String title;
23+
24+
@override
25+
_MyHomePageState createState() => _MyHomePageState();
26+
}
27+
28+
class _MyHomePageState extends State<MyHomePage> {
29+
Future _getProducts() async {
30+
// Initialize the API
31+
WooCommerceAPI wooCommerceAPI = WooCommerceAPI(
32+
url: "https://www.yourwebsite.com",
33+
consumerKey: "ck_your_consumer_key",
34+
consumerSecret: "cs_your_consumer_secret");
35+
36+
// Get data using the "products" endpoint
37+
var products = await wooCommerceAPI.getAsync("products");
38+
return products;
39+
}
40+
41+
@override
42+
Widget build(BuildContext context) {
43+
return Scaffold(
44+
appBar: AppBar(
45+
title: Text(widget.title),
46+
),
47+
body: FutureBuilder(
48+
future: _getProducts(),
49+
builder: (BuildContext context, AsyncSnapshot snapshot) {
50+
if (snapshot.hasData) {
51+
// Create a list of products
52+
return ListView.builder(
53+
itemCount: snapshot.data.length,
54+
itemBuilder: (BuildContext context, int index) {
55+
return ListTile(
56+
leading: CircleAvatar(
57+
child:
58+
Image.network(snapshot.data[index]["images"][0]["src"]),
59+
),
60+
title: Text(snapshot.data[index]["name"]),
61+
subtitle:
62+
Text("Buy now for \$ " + snapshot.data[index]["price"]),
63+
);
64+
},
65+
);
66+
}
67+
68+
// Show a circular progress indicator while loading products
69+
return Center(
70+
child: CircularProgressIndicator(),
71+
);
72+
},
73+
),
74+
);
75+
}
76+
}

ios/Runner/GeneratedPluginRegistrant.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77

88
#import <Flutter/Flutter.h>
99

10+
NS_ASSUME_NONNULL_BEGIN
11+
1012
@interface GeneratedPluginRegistrant : NSObject
1113
+ (void)registerWithRegistry:(NSObject<FlutterPluginRegistry>*)registry;
1214
@end
1315

16+
NS_ASSUME_NONNULL_END
1417
#endif /* GeneratedPluginRegistrant_h */

lib/query_string.dart

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
class QueryString {
2-
/**
3-
* Parses the given query string into a Map.
4-
*/
2+
/// Parses the given query string into a Map.
53
static Map parse(String query) {
6-
var search = new RegExp('([^&=]+)=?([^&]*)');
7-
var result = new Map();
4+
RegExp search = RegExp('([^&=]+)=?([^&]*)');
5+
Map result = Map();
86

97
// Get rid off the beginning ? in query strings.
108
if (query.startsWith('?')) query = query.substring(1);
@@ -19,4 +17,4 @@ class QueryString {
1917

2018
return result;
2119
}
22-
}
20+
}

0 commit comments

Comments
 (0)