contentful 1.0.1

contentful-dart #

pub package

This Dart package is a small abstraction on top of the Contentful Delivery API.

Usage #

To use this plugin, install contentful as a dependency in your pubspec.yaml.

API #

The following example uses equatable and json_annotation to create a Contentful Entry model. For more information about json_annotation, see: https://pub.dev/packages/json_serializable

Contentful adds system fields to their JSON responses, so you need to create a separate fields class and pass it to the Entry<T> class generic.

You can also add relationships as a field attribute and they will get injected if they are included in the JSON response. See here for more details: https://www.contentful.com/developers/docs/concepts/links/

import 'package:equatable/equatable.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:contentful/contentful.dart';

part 'event.g.dart';

@JsonSerializable()
class Event extends Entry<EventFields> {
  Event({
    SystemFields sys,
    EventFields fields,
  }) : super(sys: sys, fields: fields);

  static Event fromJson(Map<String, dynamic> json) => _$EventFromJson(json);

  Map<String, dynamic> toJson() => _$EventToJson(this);
}

@JsonSerializable()
class EventFields extends Equatable {
  EventFields({
    this.title,
    this.slug,
    this.relations,
  }) : super([title, slug, relations]);

  final String title;
  final String slug;
  final List<Event> relations;

  static EventFields fromJson(Map<String, dynamic> json) =>
      _$EventFieldsFromJson(json);

  Map<String, dynamic> toJson() => _$EventFieldsToJson(this);
}

Here is how you would use your Event class.

import 'package:contentful/contentful.dart';
import 'event.dart';

class EventRepository {
  EventRepository(this.contentful);
  final Client contentful;

  Future<Event> findBySlug(String slug) async {
    final collection = await contentful.getEntries<Event>({
      'content_type': 'event',
      'fields.slug': slug,
      'limit': '1',
      'include': '10',
    }, Event.fromJson);

    return collection.items.first;
  }
}

Future<void> main() async {
  final repo = EventRepository(Client('SPACE_ID', 'ACCESS_TOKEN'))
  final event = await repo.findBySlug('myevent');
  print('Title: ${event.fields.title}');
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  contentful: ^1.0.1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:contentful/contentful.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
60
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
30
Overall:
Weighted score of the above. [more]
66
Learn more about scoring.

We analyzed this package on Oct 16, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.1
  • pana: 0.12.21

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:contentful/contentful.dart.

Health issues and suggestions

Document public APIs. (-0.66 points)

86 out of 87 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Maintenance issues and suggestions

Provide a file named CHANGELOG.md. (-20 points)

Changelog entries help developers follow the progress of your package. See the example generated by stagehand.

Support latest dependencies. (-20 points)

The version constraint in pubspec.yaml does not support the latest published versions for 2 dependencies (equatable, json_annotation).

The package description is too short. (-20 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package.

Common filename patterns include main.dart, example.dart, and contentful.dart. Packages with multiple examples should provide example/README.md.

For more information see the pub package layout conventions.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
equatable ^0.2.4 0.2.6 0.6.1
http ^0.12.0 0.12.0+2
json_annotation ^2.4.0 2.4.0 3.0.0
meta ^1.1.6 1.1.7
Transitive dependencies
async 2.4.0
charcode 1.1.2
collection 1.14.12
http_parser 3.1.3
path 1.6.4
pedantic 1.8.0+1
source_span 1.5.5
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
Dev dependencies
build_runner ^1.0.0
json_serializable ^3.0.0