fromBytes static method

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

Implementation

static Future<LottieComposition> fromBytes(Uint8List 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));
      }
    }
  }

  return composition;
}