cosmic 1.1.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 0

Cosmic #

Simple work with REST JSON API for DART

Usage #

A simple usage example:

import 'package:http/http.dart' as http;
import 'package:cosmic/converters/cosmic_converters.dart';
import 'package:cosmic/annotations/cosmic_annotations.dart';
import 'package:cosmic/cosmic.dart' as Cosmic;   
class Fixer {
  String base;
  String date;
  Map<String, double> rates;

part 'fixer.g.dart';
  converter: const JsonConverter()
class FixerClient extends Cosmic.Client with _$FixerClient {
  Future<Fixer> latest({@Query("base") String base = "USD"});
main() async {
  Fixer latest = await fixerClient.latest();

A middleware:

FixerClient client = new FixerClient()
  ..use((request, next) {
    print("${request.httpMethod} ${request.url}");
    request.headers["token"] = "12345";

A converter:

import 'package:jsonx/jsonx.dart' as jsonx;
class JsonConverter extends Converter {
  const JsonConverter();
  dynamic decode(String data, {Type type}) => jsonx.decode(data, type: type);
  String encode(object) => jsonx.encode(object);

(check example/placeholder_client.dart for more)

Generate #

dart:mirror is absent from flutter
use cosmic:generate in order to generate the api impl pre runtime

$ pub run cosmic:generate -i example/clients/def/placeholder.dart -c PlaceholderClient -o example/clients/impl -w true

Features and bugs #

Please file feature requests and bugs at the issue tracker.

Changelog #

1.1.0 #

  • Change $name.g client to be part of the client clients must include part '$name.g.dart' and with _$NameClient (after gen)

0.0.3 #

  • Request's middlewares

0.0.2 #

  • Separate encode/decode to converters

0.0.1 #

  • Initial version, created by Stagehand


import 'clients/placeholder.dart';
import 'package:cosmic/cosmic.dart';
import 'entities/post_entity.dart';

main() async {
  PlaceholderClient client = new PlaceholderClient()
    ..use((request, next) {
      print("${request.httpMethod} ${request.url}");
      request.headers["hello"] = "world";

  client = Cosmic.create(client);

  // Get post with id: 1
  Map<String, String> headers = {
    "auth": "13325353453"
  var post_1 = await client.getPost(id: 1, headers: headers);
  print("${} - ${post_1.title}");

  // Get all posts
  var posts = await client.getPosts();
  posts.forEach((post) => print("${} - ${post.title}"));

  // Create a new post
  PostEntity post = new PostEntity()
    ..title = "Hello From Dart Cosmic"
    ..body = "foo"
    ..userId = 1;

  var postResponse = await client.create(post: post); =;
  print("${} - ${post.title}");

  // Update post = 1;
  post.body = "Hakuna matata!";
  post = await client.update(id:, post: post);
  print("${} - ${post.body}");

  // Delete
  var resp = await client.delete(id: 1);

Use this package as a library

1. Depend on it

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

  cosmic: ^1.1.0

2. Install it

You can install packages from the command line:

with pub:

$ pub get

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

3. Import it

Now in your Dart code, you can use:

import 'package:cosmic/cosmic.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

The package version is not analyzed, because it does not support Dart 2. Until this is resolved, the package will receive a health and maintenance score of 0.

Analysis issues and suggestions

Support Dart 2 in pubspec.yaml.

The SDK constraint in pubspec.yaml doesn't allow the Dart 2.0.0 release. For information about upgrading it to be Dart 2 compatible, please see


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev <2.0.0