· 6 years ago · Feb 04, 2019, 12:56 PM
1import 'package:flutter/material.dart';
2import 'package:http/http.dart' as http;
3import 'dart:convert';
4
5main() {
6 runApp(MyApp());
7}
8
9class GlobalVar {
10 //static const String Ip = "http://159.89.168.119/";
11 static const String url = "https://mkrasoi.com/";
12 static const String consumerKey =
13 "ck_0e213022b985004532347284c7c372d9e3817639";
14 static const String secretKey = "cs_372250076d2c41401657c63068b1fea5fd903260";
15}
16
17class MyApp extends StatelessWidget {
18 @override
19 Widget build(BuildContext context) {
20 return MaterialApp(
21 home: Scaffold(
22 body: Center(
23 child: RaisedButton(
24 child: Text('tap'),
25 onPressed: () {
26 getData();
27 },
28 ))));
29 }
30}
31
32Future getData() async {
33 String basicAuth = 'Basic ' +
34 base64.encode(
35 utf8.encode('${GlobalVar.consumerKey}:${GlobalVar.secretKey}'));
36
37 var response =
38 await http.get("https://mkrasoi.com/wp-json/wc/v2/orders/1453", headers: {
39 'Authorization': basicAuth,
40 'Accept': 'application/json',
41 });
42 if (response.statusCode == 200) {
43 String responseBody = response.body;
44 var data = json.decode(responseBody);
45 print(data);
46 var items = data["line_items"];
47 for (var dataItems in items) {
48 print(items);
49 for (int i = 0; i < items.length; i++) {
50 int id = dataItems["id"];
51 var name = dataItems["name"];
52 var price = dataItems['price'];
53 var quantity = dataItems['quantity'];
54 print(name.toString() +
55 '\n' +
56 price.toString() +
57 '\n' +
58 quantity.toString());
59 }
60 }
61 } else {
62 print(response.statusCode);
63 print(response.body);
64 }
65}