Skip to content

Dart / Flutter

CloudMock does not require a custom Dart SDK. Configure the AWS SDK for Dart or your HTTP client to point at the CloudMock gateway. This works for Flutter mobile apps, Flutter web, and server-side Dart.

The official AWS SDK for Dart is still in developer preview. For services that have a published Dart package, configure the endpoint:

pubspec.yaml
dependencies:
aws_s3_api: ^0.2.0 # if available
aws_dynamodb_api: ^0.2.0 # if available
aws_common: ^0.7.0
import 'package:aws_s3_api/s3-2006-03-01.dart';
import 'package:aws_common/aws_common.dart';
final credentials = AWSCredentialsProvider(AWSCredentials(
'test', // accessKeyId
'test', // secretAccessKey
));
final s3 = S3(
region: 'us-east-1',
credentialsProvider: credentials,
endpointUrl: Uri.parse('http://localhost:4566'),
);

If the official AWS Dart SDK does not cover the service you need, you can call CloudMock’s REST API directly using Dart’s http package or dio:

import 'package:http/http.dart' as http;
import 'dart:convert';
const cloudmockUrl = 'http://localhost:4566';
// Create an S3 bucket
Future<void> createBucket(String name) async {
final response = await http.put(
Uri.parse('$cloudmockUrl/$name'),
headers: {
'Authorization': 'AWS4-HMAC-SHA256 ...',
'x-amz-content-sha256': 'UNSIGNED-PAYLOAD',
},
);
print('Create bucket: ${response.statusCode}');
}
// List S3 buckets
Future<void> listBuckets() async {
final response = await http.get(
Uri.parse(cloudmockUrl),
headers: {
'Authorization': 'AWS4-HMAC-SHA256 ...',
},
);
print('Buckets: ${response.body}');
}
import 'package:dio/dio.dart';
final dio = Dio(BaseOptions(
baseUrl: 'http://localhost:4566',
headers: {
'Authorization': 'AWS4-HMAC-SHA256 ...',
},
));
// DynamoDB - CreateTable
Future<void> createTable() async {
final response = await dio.post(
'/',
data: {
'TableName': 'Users',
'KeySchema': [
{'AttributeName': 'UserId', 'KeyType': 'HASH'}
],
'AttributeDefinitions': [
{'AttributeName': 'UserId', 'AttributeType': 'S'}
],
'BillingMode': 'PAY_PER_REQUEST',
},
options: Options(headers: {
'X-Amz-Target': 'DynamoDB_20120810.CreateTable',
'Content-Type': 'application/x-amz-json-1.0',
}),
);
print('Table created: ${response.data}');
}

Several community packages provide higher-level AWS clients for Dart:

  • aws_s3_api — S3 operations with Dart types
  • aws_dynamodb_api — DynamoDB operations
  • aws_cognito_identity_provider — Cognito User Pools

Most accept an endpointUrl parameter. Set it to http://localhost:4566 (or the appropriate host for your environment).

The Android emulator cannot reach localhost on the host machine. Use 10.0.2.2 instead:

const cloudmockUrl = 'http://10.0.2.2:4566';

The iOS Simulator shares the host Mac’s network stack. localhost works directly:

const cloudmockUrl = 'http://localhost:4566';
import 'dart:io' show Platform;
String get cloudmockUrl {
if (Platform.isAndroid) {
return 'http://10.0.2.2:4566';
}
return 'http://localhost:4566';
}

When running flutter run -d chrome, the app runs in the browser on the same machine as CloudMock. Use http://localhost:4566. CORS headers are returned by CloudMock by default.

Android blocks cleartext HTTP by default. Add to android/app/src/main/AndroidManifest.xml:

<application
android:usesCleartextTraffic="true"
... >

Or use a more targeted network security config as described in the Kotlin guide.

Add to ios/Runner/Info.plist:

<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsLocalNetworking</key>
<true/>
</dict>

AWS APIs require Signature V4 signed requests. If you are calling CloudMock directly (without an AWS SDK), you have two options:

  1. Set IAM mode to none in CloudMock configuration, which bypasses all authentication:

    iam:
    mode: none

    Then you can make unsigned requests. This is the simplest approach for development.

  2. Use an AWS SigV4 signing library for Dart. The aws_common package provides signing utilities.

When using an official AWS SDK package with endpointUrl, signing is handled automatically.

The official AWS SDK for Dart is in developer preview. Not all services have published packages. For services without a Dart SDK, use direct HTTP calls with the X-Amz-Target header for JSON-protocol services or REST-style URLs for REST-XML services.

Use 10.0.2.2 for Android Emulator and localhost for iOS Simulator. Physical devices require the host machine’s IP on the local network.