Search code examples
wordpressrestflutterfutureflutter-futurebuilder

instance of 'X' even after using FutureBuilder Flutter


I have a flutter app that uses the [flutter_wordpress] package to make HTTP requests to WordPress REST API v2. Everything is great except for the fact that I get ''instance of Categoryeven after using theFutureBuilder`` widget

backend


import 'package:flutter_wordpress/flutter_wordpress.dart' as wp;
import 'package:saviortv_v2/backend/WordPressConfiguration.dart';

Future<List<wp.Category>> fetchCategories(Map<String, int> args) async {
  var categories = wordPress.fetchCategories(
    params: wp.ParamsCategoryList(
      order: wp.Order.desc,
      pageNum: args['pageNum'],
      perPage: args['perPage'],
    ),
  );
  return categories;
}

FutureBuilder


import 'package:saviortv_v2/backend/WordPressCategories.dart' as wo;

class HorizontalSlider extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: wo.fetchCategories({'perPage': 5, 'pageNum': 1}),
      builder: (context, snapshot) {
        print(snapshot.data);
        return Container(
          child: Text(snapshot.toString()),
        );
      },
    );
  }
}

Here is my debug console

enter image description here

The reason why those future instances are inside of an array is because my fetchCategories function returns a list of categories...

How can I solve the problem? How can I show the real categories instead of the ``instance of 'Category'?


Solution

  • This has nothing to do with futures. You have a list of structures. You don't just call toString() on it and expect it to transform magically into a list of controls that you would like. You have to program it.

    To display lists of data, you can use a ListView.

    For example, like this:

    ListView.builder(
          itemCount: snapshot.data.length,
          itemBuilder: (context, index) => Text('${snapshot.data[index]}'),
            );
    

    Now this will only give you a list with items that say "Instance of 'Category', it's your job to find out what of the Category data structure you actually want to display. Maybe it has a 'Name' element?