【问题标题】:RangeError (end): Invalid value: Not in inclusive range 0..70: -1 flutterRangeError(结束):无效值:不在包含范围内 0..70:-1 颤动
【发布时间】:2022-01-25 15:25:37
【问题描述】:

我正在尝试使用 flutter image compress package 压缩图像

Future<File> testCompressAndGetFile(File file) async {

  final lastIndex = file.absolute.path.lastIndexOf(new RegExp(r'.jp')); //this returns -1

    final splitted = file.absolute.path.substring(0, (lastIndex)); // I always get the error from here

    String oP = "${splitted}_ql${file.absolute.path.substring(lastIndex)}";

    final result = await FlutterImageCompress.compressAndGetFile(
      file.absolute.path,
      oP,
      quality: 95,
    );

    return result;
}

当我打印 lastIndex 时,我得到的 -1 不在 0-70 的范围内。 并因此 splitted 引发错误

RangeError (end): Invalid value: Not in inclusive range 0..70: -1

请问我该如何解决这个错误

【问题讨论】:

  • 问题主要出在您的文件与正则表达式相结合。您尝试使用此函数压缩的文件及其路径是什么?

标签: flutter dart


【解决方案1】:

lastIndexOf 中的lastIndex-1 值表示未找到该模式。所以.jp 实际上不在您的输入中。如果这是一个有效的用例,您应该提前保护并返回或抛出异常。

对于它的价值,String 实现了Pattern,因此无需为此模式实例化RegExp。这会很好用。 .lastIndexOf('.jp').

【讨论】:

  • ok 会试试的。谢谢
猜你喜欢
  • 2022-11-23
  • 2021-08-14
  • 2021-10-07
  • 2021-07-08
  • 2021-12-23
  • 2021-02-04
  • 1970-01-01
  • 2021-03-21
  • 2021-10-21
相关资源
最近更新 更多