Skip to main content

brix_network 1.2.3 copy "brix_network: ^1.2.3" to clipboard
brix_network: ^1.2.3 copied to clipboard

REST calls for brix on Dio: every call returns a Result, with retries, uploads, downloads, backend messages, field errors and one logout on 401.

example/main.dart

// brix_network against a public test API.
//
// Run: dart run example/main.dart
import 'package:brix_network/brix_network.dart';

class Todo {
  Todo({required this.id, required this.title, required this.done});

  factory Todo.fromJson(Json json) => Todo(
    id: json.integer('id'),
    title: json.string('title'),
    done: json.boolean('completed'),
  );

  final int id;
  final String title;
  final bool done;
}

Future<void> main() async {
  // This API answers with the data itself, no {data: ...} envelope.
  final api = BrixApi(
    baseUrl: 'https://jsonplaceholder.typicode.com/',
    reader: ResponseReader.custom((response) => ApiReply(data: response.body)),
    logger: PrettyNetworkLogger(maxLines: 8),
  );

  final todos = await api.get(
    'todos',
    query: {'_limit': 3},
    parse: (data) => data.items(Todo.fromJson),
  );

  switch (todos) {
    case Success(:final data):
      for (final todo in data) {
        print('${todo.done ? 'โœ“' : 'ยท'} ${todo.title}');
      }
    case Failed(:final failure):
      print('Could not load: ${failure.displayMessage}');
  }

  api.close();
}
1
likes
160
points
1.12k
downloads
screenshot

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

REST calls for brix on Dio: every call returns a Result, with retries, uploads, downloads, backend messages, field errors and one logout on 401.

Repository (GitHub)
View/report issues

Topics

#network #http #dio #rest-api #api

License

MIT (license)

Dependencies

brix, dio, meta

More

Packages that depend on brix_network