我正在尝试在 dart 中解码一些动画 webp 文件,但出现“索引超出范围”的运行时错误,它应该小于 N。N 是文件的大小(以字节为单位)。但除 this folder 以外的图像工作正常。
我使用的动画 webp 文件的链接 https://github.com/WhatsApp/stickers/tree/main/Android/app/src/main/assets/2
Dart 代码:
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()
}
控制台:
Exception has occurred.
IndexError (RangeError (index): Index out of range: index should be less than 5292: 5292)
工具版本:
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:我查过similar question on stackoverflow但这是基于 Flutter UI 组件的,因此组件引起的错误和答案仅与 UI 组件相关。
最佳答案
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)!;
也许,这会解决你的问题
关于flutter - Dart : Index out of range: index should be less than N: N (where N is the size of file in bytes). 如何修复此运行时错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71265285/