fromBytes static method

Future<LottieComposition> fromBytes(
  1. List<int> bytes, {
  2. String? name,
  3. LottieImageProviderFactory? imageProviderFactory,
})

Implementation

static Future<LottieComposition> fromBytes(List<int> bytes,
    {String? name, LottieImageProviderFactory? imageProviderFactory}) async {
  Archive? archive;
  if (bytes[0] == 0x50 && bytes[1] == 0x4B) {
    archive = ZipDecoder().decodeBytes(bytes);
    var jsonFile = archive.files.firstWhere((e) => e.name.endsWith('.json'));
    bytes = jsonFile.content as Uint8List;
  }

  var composition = LottieCompositionParser.parse(
      LottieComposition._(name), JsonReader.fromBytes(bytes));

  if (archive != null) {
    for (var image in composition.images.values) {
      var imagePath = p.posix.join(image.dirName, image.fileName);
      var found = archive.files.firstWhereOrNull(
          (f) => f.name.toLowerCase() == imagePath.toLowerCase());

      ImageProvider? provider;
      if (imageProviderFactory != null) {
        provider = imageProviderFactory(image);
      }

      if (provider != null) {
        image.loadedImage = await loadImage(composition, image, provider);
      }

      if (found != null) {
        image.loadedImage ??= await loadImage(
            composition, image, MemoryImage(found.content as Uint8List));
      }
    }

    for (var font in archive.files.where((f) => f.name.endsWith('.ttf'))) {
      var fileName = p.basenameWithoutExtension(font.name).toLowerCase();
      var existingFont = composition.fonts.values
          .firstWhereOrNull((f) => f.family.toLowerCase() == fileName);
      await loadFontFromList(font.content as Uint8List,
          fontFamily: existingFont?.family);
    }
  }

  return composition;
}