Dart avatar library
Generate avatars in Dart (3.4 or higher) and in Flutter apps, with no external service involved.
The API mirrors the JavaScript library, and the output is byte-identical: the same seed and style produce the same SVG in every DiceBear library.
Installation
You need two packages: the core library dicebear_core and the avatar style definitions dicebear_styles. Each style is a string constant in its own library, so a compiled app only embeds the styles it imports.
dart pub add dicebear_core
dart pub add dicebear_stylesIn a Flutter project, use flutter pub add instead.
Usage
Each style is exposed as a raw-JSON string (e.g. lorelei from package:dicebear_styles/lorelei.dart) that you hand to Style.parse:
import 'package:dicebear_core/dicebear_core.dart';
import 'package:dicebear_styles/lorelei.dart';
void main() {
final style = Style.parse(lorelei);
final avatar = Avatar(style, {
'seed': 'John',
// ... other options
});
print(avatar.svg);
}Every style has its own options, listed on its style page.
Info
The avatar styles come from many creators, and each creator chooses the license for their own style. The license overview lists them all in one place.
Types
Styleis a validated, immutable wrapper around a style definition.Style.parsedecodes a raw JSON string, the defaultStyle(...)constructor takes an already decodedMap<String, Object?>. Build it once and reuse it for every avatar of that style. Invalid definitions throw aStyleValidationError.Avatarrenders one avatar from aStyleand an optional map of options. Invalid options throw anOptionsValidationError, circular color references aCircularColorReferenceError.OptionsDescriptordescribes all valid options of a style, for building UIs or validating user input:OptionsDescriptor(style).toJson(). See Style options.
Methods
| Method | Returns |
|---|---|
svg or toString() | The SVG as a String, so an Avatar works directly in string interpolation and print |
toJson() | A Map<String, Object?> with the SVG under svg and the resolved options under options, for jsonEncode |
resolvedOptions | The resolved options as a map |
toDataUri() | The SVG as a data URI |
Core options
These options are the same across every DiceBear core. See Core options for the full reference. Here are the options in Dart syntax:
final avatar = Avatar(style, {
'seed': 'Alice',
'flip': 'horizontal', // 'none', 'horizontal', 'vertical', 'both'
'rotate': 10, // -360 to 360, or [min, max] range
'scale': 0.9, // 0 to 10 (1 = original), or [min, max] range
'borderRadius': 50, // 0-50 (50 = circle)
'size': 128,
'translateX': 0, // -1000 to 1000 (percent of canvas width)
'translateY': 0, // -1000 to 1000 (percent of canvas height)
'idRandomization': true,
'title': 'User Avatar',
'fontFamily': 'Arial', // or ['Arial', 'Helvetica'], one per seed
'fontWeight': 700, // 1-1000
'backgroundColor': ['#b6e3f4', '#c0aede'],
'backgroundColorFill': 'solid', // 'solid', 'linear', 'radial'
});Dynamic component and color options also work the same way. See Dynamic component options for all available patterns.
Examples
Multiple avatars on the same page
When you inline several avatars into one page, use idRandomization to keep their SVG IDs and animation names from colliding:
for (final seed in ['alice', 'bob', 'charlie']) {
final avatar = Avatar(style, {
'seed': seed,
'idRandomization': true,
});
print(avatar.svg);
}Weighted variant selection
A weight map makes some variants more likely than others. Here one in five critters has a single eye:
import 'package:dicebear_styles/critters.dart';
final style = Style.parse(critters);
final avatar = Avatar(style, {
'seed': 'Alice',
'eyesVariant': {'round': 4, 'mono': 1},
});