flutter - Dart : Index out of range: index should be less than N: N (where N is the size of file in bytes). 如何修复此运行时错误?

标签 flutter dart uint8t

我正在尝试在 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/

相关文章:

flutter - Flutter-如何使用某种生成器在同一段落中显示具有不同样式的字符串数组?

c - uint8 与 C 中的 scanf 和 printf

firebase - Flutter firebase 软件包版本在消息传递、核心和分析之间发生冲突

flutter - BoxDecoration 中的 FadeInImage

firebase - 在 Flutter App 上更新 FCM Token

firebase - 如何在Flutter中从Firebase Cloud Firestore查询数据

android - 如何更新购物车中的项目数?

c++ - 无法通过仅给出大小在 CPP std::vector<std::vector<uint8_t>> 中创建二维 vector

c++ - uint8_t和unsigned char之间的混淆

android - 未处理的异常 : PlatformException(sign_in_failed, com.google.android.gms.common.api.ApiException : 12500: , 空)