mobile_api 0.0.8+15 copy "mobile_api: ^0.0.8+15" to clipboard
mobile_api: ^0.0.8+15 copied to clipboard

Typed REST and GraphQL API helpers for Flutter apps, with token refresh, secure cache storage, multipart uploads, and network checks.

example/main.dart

import 'package:flutter/foundation.dart';
import 'package:mobile_api/mobile_api.dart';

class UserProfile {
  const UserProfile({this.id, this.name});

  factory UserProfile.fromJson(Map<String, dynamic> json) {
    return UserProfile(
      id: json['id'] as String?,
      name: json['name'] as String?,
    );
  }

  final String? id;
  final String? name;
}

Future<void> main() async {
  final cache = ICacheRepoImpl();
  await cache.saveAll(
    CacheKeyBundle.tokenPair(access: 'access-token', refresh: 'refresh-token'),
  );

  final config = ApiConfig(
    apiUrl: Uri.parse('https://api.example.com'),
    refreshTokenPath: '/api/accounts/refresh',
    appCache: cache,
    checkNetwork: CheckNetwork(),
    headers: const {
      HttpHeadersConst.marketplace: 'ml',
      HttpHeadersConst.userAgent: 'mlApp',
      HttpHeadersConst.acceptLanguage: 'en',
    },
  );

  final rest = IHttpImpl(apiConfig: config);
  final profileResult = await rest
      .baseMethod<UserProfile, DefaultErrorResponse>(
        '/api/users/me',
        requestType: RequestType.get,
        dataFromJson: UserProfile.fromJson,
        errorFromJson: DefaultErrorResponse.fromJson,
      );

  switch (profileResult) {
    case Success(value: final profile):
      debugPrint('Hello ${profile.name ?? profile.id}');
    case Failure(exception: final error):
      debugPrint('REST request failed: ${error.reasonPhrase}');
  }

  final graphQl = IGraphQlImpl(
    apiConfig: config.copyWith(
      apiUrl: Uri.parse('https://api.example.com/graphql'),
    ),
  );

  final usersResult = await graphQl
      .queryList<UserProfile, DefaultErrorResponse>(
        path: '''
query users {
  users {
    pageInfo { hasNextPage }
    items { id name }
  }
}
''',
        field: 'users',
        dataFromJson: UserProfile.fromJson,
        errorFromJson: DefaultErrorResponse.fromJson,
      );

  switch (usersResult) {
    case Success(value: final users):
      debugPrint('Loaded ${users.length} users');
    case Failure(exception: final error):
      debugPrint('GraphQL request failed: ${error.reasonPhrase}');
  }
}
1
likes
0
points
39
downloads

Publisher

unverified uploader

Weekly Downloads

Typed REST and GraphQL API helpers for Flutter apps, with token refresh, secure cache storage, multipart uploads, and network checks.

Homepage
Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

connectivity_plus, flutter, flutter_secure_storage, fresh_graphql, graphql, http

More

Packages that depend on mobile_api