pub_api_client icon indicating copy to clipboard operation
pub_api_client copied to clipboard

feature request: expand fetchFlutterFavorites

Open polilluminato opened this issue 2 years ago • 3 comments

Hi everyone, for one of my side projects I'm using this package to create a desktop app to browse pub.dev using the same concept of the AppStore, Windows Store, or other stores on Linux distros.

In my case I need to create a section with the list of packages of the Flutter Favorite program and those associated with Google that can be retrieved respectively with this two APIs:

  • fetchGooglePackages
  • fetchFlutterFavorites

Unfortunately, these two functions only return the List <String> with the package names while in my case I would need the complete information about the package represented by the PubPackage object. Right now I solved this issue by calling the fetchFlutterFavorites and after that in another widget call the packageInfo API to get all the info, something like that using Riverpod:


final allFlutterFavoritesProvider = FutureProvider.autoDispose<List<String>>((ref) async {
  return await PubClient().fetchFlutterFavorites();
});

final singlePackageProvider = FutureProvider.autoDispose.family<PubPackage, String>((ref, packageName) async {
  return await PubClient().packageInfo(packageName);
});

[...]

Widget build(BuildContext context, WidgetRef ref) {
    AsyncValue<List<String>> allFlutterFavoritesList = ref.watch(allFlutterFavoritesProvider);

    return Column(
      children: [
        ...
        allFlutterFavoritesList.when(
          loading: ...
          error: ...
          data: (list) {
            return GridView.builder(
              shrinkWrap: true,
              gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: 3,
                  crossAxisSpacing: kDefaultPagePadding,
                  mainAxisSpacing: kDefaultPagePadding),
              itemCount: list.length,
              itemBuilder: (_, int index) {
                return PackageGridBox(
                  packageName: list.elementAt(index),
                );
              },
            );
          },
        ),
      ],
    );
  }

[...]

class PackageGridBox extends ConsumerWidget {
  const PackageGridBox({Key? key, required this.packageName}) : super(key: key);

  final String packageName;

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    AsyncValue<PubPackage> singlePackage = ref.watch(singlePackageProvider(packageName));

    return singlePackage.when(
      loading: ...
      error: ...
      data: (package) {
        return Container(
          decoration: kDefaultBoxDecoration(context),
          child: Text(package.toString()),
        );
      },
    );
  }
}

Do you think it might be useful to add an API that allows you to immediately have all the information about the packages and not just the package name? Something like that:

/// Retrieves all the flutter favorites PubPackage
Future<List<PubPackage>> fetchFlutterFavoritesPubPackage() async {
  final searchResults = await search('is:flutter-favorite');
  final results = await recursivePaging(this, searchResults);
  return await Future.wait(
    results.map((r) => packageInfo(r.package)).toList());
}

I know that for all the packages you must wait for all the packageInfo calls but it might be useful to someone.

Best regards, Alberto

polilluminato avatar May 25 '22 17:05 polilluminato

@polilluminato this is an ongoing challenge with the API. I had to do something similar for the flutter.space API.

https://github.com/fluttertools/flutter.space/blob/main/lib/helpers.dart#L114

I think a helper function might be interesting, let me know if the link provided is something that it would work.

leoafarias avatar May 25 '22 17:05 leoafarias

@leoafarias you recommend using flutter-favorites.json to get information on Flutter Favorites Packages to show in a list and then use the pub_api_client package to get more information on a detail page?

In this way, I don't have to change the behavior of the pub_api_client package. I can use both the flutter.space endpoints to get lists (favorite, google, popular, most liked, and so on) and pub_api_client package to get detailed information.

Sounds good to me 😀

polilluminato avatar May 25 '22 19:05 polilluminato

Yes feel free to use whatever you need for Flutter Spaces, if you end up with flutter space SDK, feel free to do a PR!

leoafarias avatar May 25 '22 20:05 leoafarias