fromAssets static method
Future<TrustPinConfiguration>
fromAssets({
- String assetPath = 'trustpin.json',
- AssetBundle? bundle,
Loads a configuration from a JSON asset bundled with the Flutter app.
Reads the asset at assetPath using the Flutter bundle (defaults to
rootBundle) and parses it according to the same schema used by the
Android native SDK's TrustPinConfiguration.fromAssets.
Asset Declaration
Declare the asset in your pubspec.yaml:
flutter:
assets:
- trustpin.json
JSON Schema
{
"organization_id": "your-org-id",
"project_id": "your-project-id",
"public_key": "MFkwEwYH...",
"mode": "strict",
"configuration_url": "https://custom.example.com/config/signed.b64"
}
| Field | Required | Notes |
|---|---|---|
organization_id |
yes | Non-empty string |
project_id |
yes | Non-empty string |
public_key |
yes | Base64-encoded verification key |
mode |
no | "strict" (default) or "permissive" |
configuration_url |
no | HTTPS URL for self-hosted configs; empty treated as unset |
Unknown top-level keys are ignored for forward compatibility.
Example
final config = await TrustPinConfiguration.fromAssets();
await TrustPin.shared.setup(config);
Throws TrustPinException with code INVALID_PROJECT_CONFIG if the
asset is missing, malformed, or fails schema validation.
Implementation
static Future<TrustPinConfiguration> fromAssets({
String assetPath = 'trustpin.json',
AssetBundle? bundle,
}) async {
final loader = bundle ?? rootBundle;
final String raw;
try {
raw = await loader.loadString(assetPath, cache: false);
} catch (e) {
throw TrustPinException(
'INVALID_PROJECT_CONFIG',
'Failed to load TrustPin configuration asset "$assetPath": $e',
);
}
final dynamic decoded;
try {
decoded = jsonDecode(raw);
} on FormatException catch (e) {
throw TrustPinException(
'INVALID_PROJECT_CONFIG',
'TrustPin configuration at "$assetPath" is not valid JSON: ${e.message}',
);
}
if (decoded is! Map<String, dynamic>) {
throw TrustPinException(
'INVALID_PROJECT_CONFIG',
'TrustPin configuration at "$assetPath" must be a JSON object.',
);
}
final organizationId =
_requireNonEmptyString(decoded, 'organization_id', assetPath);
final projectId = _requireNonEmptyString(decoded, 'project_id', assetPath);
final publicKey = _requireNonEmptyString(decoded, 'public_key', assetPath);
final mode = _parseMode(decoded['mode'], assetPath);
final configurationURL =
_parseConfigurationUrl(decoded['configuration_url'], assetPath);
return TrustPinConfiguration(
organizationId: organizationId,
projectId: projectId,
publicKey: publicKey,
mode: mode,
configurationURL: configurationURL,
);
}