Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Authentication #3

Closed
farisabbasi opened this issue Oct 9, 2018 · 1 comment
Closed

Authentication #3

farisabbasi opened this issue Oct 9, 2018 · 1 comment

Comments

@farisabbasi
Copy link

I copied your code but my app is stuck on Loading text its not fetching products

here's my code

`import 'dart:async';

import 'package:flutter/material.dart';
import 'package:woocommerce_api/woocommerce_api.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
@OverRide
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'WooCommerce API Demo'),
);
}
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);

final String title;

@OverRide
_MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State {

List products = [];

Future getProducts() async {

/// Initialize the API
WooCommerceAPI wc_api = new WooCommerceAPI(
    "http://localhost/ropaarmario/wp-json/wc/v2/products",
    "ck_87f26757f5b65a28f50b9fb5b460793abb6f1d93",
    "cs_fa87963767fbe06b60fa23da38f31d514c51e2a3"
);

/// Get data using the endpoint
var p = await wc_api.getAsync("products");
return p;

}

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

@OverRide
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
),
body: FutureBuilder(
future: getProducts(),
builder: (_, s){

      if(s.data == null){
        return Container(
          child: Center(
            child: Text("Loading..."),
          ),
        );
      }

      return ListView.builder(
          itemCount: s.data.length,
          itemBuilder: (_, index){

            /// create a list of products
            return ListTile(
              leading: CircleAvatar(
                child: Image.network(s.data[index]["images"][0]["src"]),
              ),
              title: Text(s.data[index]["name"]),
              subtitle: Text("Buy now for \$ " + s.data[index]["price"]),
            );

          }
      );
    },
  ),
);

}
}
`
i checked on postman its working through oauth authentication method

@samarthagarwal
Copy link
Owner

Keep your url http://localhost/ropaarmario only. No need to append the wp-json/wc/v2 part.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants