2

I am trying to decode some animated webp files in dart but getting the runtime error of 'index out of range and it should be less than N. and N is the size of file in bytes. But images other than this folder works fine.

Link of animated webp files I used https://github.com/WhatsApp/stickers/tree/main/Android/app/src/main/assets/2

Dart Code:

import 'dart:io';
import 'package:image/image.dart';

void main() {
  final image = 'asset/ok.webp';
  File bytes = File(image);
  List<int> list = bytes.readAsBytesSync(); 
  // list.length = 85538 

  Animation anim = decodeWebPAnimation(list)!; 
  // runtime error raised from decodeWebPAnimation()
  
}

Console:

Exception has occurred.
IndexError (RangeError (index): Index out of range: index should be less than 5292: 5292)

Tools versions:

Flutter 2.10.2 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 097d3313d8 (6 days ago) • 2022-02-18 19:33:08 -0600
Engine • revision a83ed0e5e3
Tools • Dart 2.16.1 • DevTools 2.9.2

PS: I have checked similar question on stackoverflow but that is based on the Flutter UI components so The error causing from the component and the answers are related to the UI components only.

CrackerKSR
  • 1,380
  • 1
  • 11
  • 29
  • 1
    have you tried with another asset image? Otherwise can you post a link to the asset you're trying to decode? – ManuH68 Feb 25 '22 at 13:03
  • I have added the link of file in question : https://raw.githubusercontent.com/WhatsApp/stickers/main/Android/app/src/main/assets/2/07_OK.webp – CrackerKSR Feb 25 '22 at 13:07
  • 1
    ah sorry, missed that. I tried with another file (ex: https://www.gstatic.com/webp/gallery/1.webp ) I get no error so my guess is that there's either something wrong with your file or you uncovered a bug in the library in which case you need to submit it to their authors. – ManuH68 Feb 25 '22 at 13:44
  • Thanks for response. Yes. I tried wtih your file. It works but it is static. So i tried with another animated webp file and that also working and I checked the length, it is 429422. which is greater than 85538. So the issue is with file. But I wonder why so. I have used same file with React Native, JAVA, etc and no issue encountered. It seems it is a bug in Dart:Image library or something I missing . – CrackerKSR Feb 25 '22 at 13:56
  • and Just noticed that the any animated webp file from the whole asset folder is causing the error. asset Folder: https://github.com/WhatsApp/stickers/tree/main/Android/app/src/main/assets/2 while another asset folder which contains static files does not cause the error – CrackerKSR Feb 25 '22 at 14:13
  • 1
    You should report the issue here: https://github.com/brendan-duncan/image/issues – ManuH68 Feb 25 '22 at 14:19
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/242407/discussion-between-crackerksr-and-manuh68). – CrackerKSR Feb 25 '22 at 14:34

1 Answers1

2

import 'package:flutter/services.dart' show rootBundle;
import 'package:path_provider/path_provider.dart'

final image = await rootBundle.load('images/07_OK.webp');
Directory pa = await p.getTemporaryDirectory();
File file = File(pa.path+"/xyz.webp");
await file.writeAsBytes(image.buffer.asUint8List(image.offsetInBytes, image.lengthInBytes));
List<int>  list = await file.readAsBytes();
Animation anim = decodeWebPAnimation(list)!;

Maybe, This will solve your problem