mastodon_dart 1.0.13 copy "mastodon_dart: ^1.0.13" to clipboard
mastodon_dart: ^1.0.13 copied to clipboard

The unofficial Dart library for accessing the Mastodon API through REST or WebSockets

example/main.dart

import 'dart:io';

import 'package:mastodon_dart/mastodon_dart.dart';
import 'package:mastodon_dart/src/exception.dart';

/// This example shows how to use the Mastodon Dart library.
///
/// Run from a terminal with:
/// BEARER_TOKEN="TOKEN" WEBSITE="https://example.social" dart run example/main.dart

main() async {
  Map<String, String> env = Platform.environment;
  final String? bearerToken = env['BEARER_TOKEN'];
  final Uri? website = Uri.tryParse(env['WEBSITE'] ?? '');

  if (bearerToken == null || bearerToken.isEmpty) {
    print('BEARER_TOKEN environment variable not set.');
    exit(1);
  }
  if (website == null) {
    print('WEBSITE environment variable not set.');
    exit(1);
  }

  final client = Mastodon(website);
  client.token = bearerToken;

  try {
    var currentAccount = await client.verifyCredentials();
    print('Hello ${currentAccount.username}!');
    print('\n');
  } on MastodonException catch (e) {
    print(e.message);
    exit(1);
  }

  var timeline = await client.timeline(limit: 5);
  for (var status in timeline) {
    print('@${status.account.acct}: ${_stripHtml(status.content)}');
  }
}

/// This is just to create a cleaner output.
/// Don't actually parse HTML with regex.
_stripHtml(String html) {
  RegExp exp = RegExp(r"<[^>]*>", multiLine: true, caseSensitive: true);
  return html.replaceAll(exp, '');
}
7
likes
130
pub points
15%
popularity

Publisher

unverified uploader

The unofficial Dart library for accessing the Mastodon API through REST or WebSockets

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

http, json_annotation, web_socket_channel

More

Packages that depend on mastodon_dart