最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Loading Network Images in Flutter test (for generating screenshots) - Stack Overflow

programmeradmin5浏览0评论

I find the idea to use goldens for creating screenshots quite intriguing. It is so much faster, more convenient and easier to work with than the screenshots package. Also by mocking the providers the screenshots have exactly the data I want.

The challenge are all the network images used across the app. Even when allowing http requests the following code will fail:

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

class SimpleImageWidget extends StatelessWidget {
  final String imageUrl;

  const SimpleImageWidget({super.key, required this.imageUrl});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Imagework(imageUrl),
      ),
    );
  }
}

void main() {

  setUpAll(() async {
    HttpOverrides.global = null;
  });


  testWidgets('Simple Image Widget Test', (WidgetTester tester) async {
    // Use a reliable image URL for testing
    const imageUrl = '/200/300';

    await tester.pumpWidget(const SimpleImageWidget(imageUrl: imageUrl));

    // Wait for the image to load
    await tester.pumpAndSettle(const Duration(seconds: 10), EnginePhase.sendSemanticsUpdate, const Duration(seconds: 20));

    // Capture the golden
    await expectLater(
      find.byType(MaterialApp),
      matchesGoldenFile('goldens/simple_image_widget.png'),
    );
  });
}

The golden is pure white and there is an error

A Timer is still pending even after the widget tree was disposed.

Just to be clear: I know that there are several packages that mock the Image and deliver a transparent image. While this clearly works it is - for the sake of screenshots - not helpful.

I also tried CachedNetworkImage without success, was pumping the Widgets several times and mocking all images individually seems quite cumbersome.

I was wondering if someone has experience with this topic or a smart idea how to work around it.

Please share your opinion, Thanks a lot in advance!

发布评论

评论列表(0)

  1. 暂无评论