wordpress_api 0.1.1 copy "wordpress_api: ^0.1.1" to clipboard
wordpress_api: ^0.1.1 copied to clipboard

outdated

A WordPress API client for flutter with support for WooCommerce and custom namespaces.

example/lib/main.dart

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

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'WPAPI Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'WP API Demo'),
    );
  }
}

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

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int page = 0;
  List posts = [];
  bool fetching = false;
  bool canFetch = true;
  WordPressAPI api = WordPressAPI('site.com');

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: ListView.builder(
        itemBuilder: (BuildContext context, int index) {
          Map<String, dynamic> post = posts[index];
          return Container(
            color: Colors.white,
            margin: EdgeInsets.all(8.0),
            child: ListTile(
              title: Padding(
                padding: const EdgeInsets.symmetric(vertical: 8.0),
                child: Text(
                  post['title']['rendered'],
                ),
              ),
              subtitle: Text(
                post['excerpt']['rendered'],
              ),
            ),
          );
        },
        itemCount: posts.length,
      ),
      floatingActionButton: FloatingActionButton.extended(
        onPressed: _fetchPosts,
        tooltip: 'Fetch Posts',
        icon: Icon(Icons.arrow_downward), label: Text('fetch more'),
      ),
    );
  }

  void _fetchPosts() async {
    // print('FETCH POSTS CALLED');
    if (canFetch && !fetching) {
      // print('BEGINING FETCH');
      setState(() {
        fetching = true;
        page++;
      });
      // print('FETCH POSTS CALLED');
      final res = await api.getAsync('posts?page=$page');
      final data = res['data'];
      final total = res['meta']['total'];
      // print(res);
      // print('DATA: $data');
      if (data != null) {
        setState(() {
          posts.addAll(data);
          page++;
          fetching = false;
        });
      }

      if (posts.length == total) {
        setState(() {
          canFetch = false;
        });
        // print('FETCHING DISABLED');
      }
      // print('DONE FETCHING');
    }
  }
}
99
likes
0
pub points
76%
popularity

Publisher

verified publisherdhmgroup.net

A WordPress API client for flutter with support for WooCommerce and custom namespaces.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter, http

More

Packages that depend on wordpress_api