以下のページを参考にしてアセット画像の縦と横の長さを比較するユニットテストを作成しました。
How do I determine the width and height of an image in Flutter? - Stack Overflow
import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
Future<void> main() async {
test('testImageWidthAndHeight', () async {
final image = File('images/condition_diagram.png');
// ここで落ちる。
final decodedImage = await decodeImageFromList(image.readAsBytesSync());
print(decodedImage.width);
print(decodedImage.height);
print('loadAssetImage実行前のwidth: ${decodedImage.width}');
print('loadAssetImage実行前のheight: ${decodedImage.height}');
expect(decodedImage.width, greaterThan(decodedImage.height));
});
}
しかし、下記のようなエラーが出力されます。
Null check operator used on a null value
package:flutter/src/painting/image_decoder.dart 24:56 decodeImageFromList
test/unit/qr_test.dart 11:32 main.<fn>
test/unit/qr_test.dart 8:32 main.<fn>
エラーが起きている行とエラー文から推察するに、decodeImageFromList関数でうまく返り値を取得できずにnullが入ってしまい、エラーになっているようです。
当然、condition_diagram.pngがimagesディレクトリにあることも確認しており、pubspec.yamlにそのパスも記載しています。
このようなキーワードで調べてみましたが、解決できませんでした。
- Null check operator used on a null value
package:flutter/src/painting/image_decoder.dart 24:56 decodeImageFromList