Flutter Web is borking out After Flutter Update

I recently upgraded my flutter and Flutter Web is super borking out and not sure where to start my troubleshooting. Here is my current flutter version:

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.27.0, on macOS 15.1.1 24B91 darwin-arm64, locale en-US)
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 16.2)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2023.3)
[✓] VS Code (version 1.96.0)
[✓] Connected device (5 available)
[✓] Network resources

My app is an iPhone and Android app but I have a beta version of it on web as well. I went to update the site after upgrading flutter and things went sideways real fast.

But this is what I am getting:

Whats interesting it seems like the images I have for items in my listview dont load until I scroll. Once I scroll that when it breaks.

  • I have more images but new users can only post one :frowning:
1 Like

This is the image i couldn’t post due to new user restrictions. This is what it should look like.

1 Like

Yea I had the same issue yesterday

I was using cached network image package for images…and even if it wasn’t that if u have images being loaded it looks sooo weird…I tried everything to make it work gpt got some solutions which I’m not happy about but it restores it to normal…so how I fixed it is hold on lemme

so i created a new file called cross_origin.dart in it

import 'dart:async';
import 'dart:typed_data';
import 'dart:ui' as ui;
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:web/web.dart' as web;

class CrossOriginNetworkImage extends ImageProvider<CrossOriginNetworkImage> {
  final String url;

  CrossOriginNetworkImage(this.url);

  @override
  Future<CrossOriginNetworkImage> obtainKey(ImageConfiguration configuration) {
    return SynchronousFuture(this);
  }

  @override
  ImageStreamCompleter loadImage(
      CrossOriginNetworkImage key, ImageDecoderCallback decode) {
    final completer = Completer<ImageInfo>();

    final imageElement = web.HTMLImageElement()
      ..src = url
      ..crossOrigin = 'anonymous';

    imageElement.onLoad.listen((_) async {
      final canvas = web.HTMLCanvasElement();
      canvas.width = imageElement.width;
      canvas.height = imageElement.height;

      final ctx = canvas.context2D;
      ctx.drawImage(imageElement, 0, 0);

      // Convert canvas to a data URL and extract bytes.
      final dataUrl = canvas.toDataUrl('image/png');

      final bytes = UriData.parse(dataUrl).contentAsBytes();

      // Decode image bytes into a Flutter ui.Image.
      final codec = await ui.instantiateImageCodec(Uint8List.fromList(bytes));
      final frame = await codec.getNextFrame();

      completer.complete(ImageInfo(image: frame.image, scale: 1.0));
    });

    imageElement.onError.listen((_) {
      completer.completeError(Exception("Failed to load image: $url"));
    });

    return OneFrameImageStreamCompleter(completer.future);
  }
}

and then I replaced all my images that were CachedImageNetworks to include

CachedNetworkImage(
                                        imageUrl: _images[index].imageUrl!,
                                        fit: BoxFit.cover,
                                        imageBuilder: (context, imageProvider) {
                                          return Image(
                                            image: CrossOriginNetworkImage(
                                                _images[index].imageUrl!),
                                            fit: BoxFit.cover,
                                          );
                                        },
                                        placeholder: (context, url) => Center(
                                          child: SpinKitPulse(
                                            color: Colors.amber[100],
                                            size: 50.0,
                                          ),
                                        ),
                                        errorWidget: (context, url, error) =>
                                            const Icon(Icons.error),
                                      )

the change is on the image builder which u didn’t need

I don’t even know how this fixed it…im just working on another fix the issue seems to be CORS problem…but i did everything and still nothing happened so i had to do it this way… Please let me know if u have come up with a better solution…I don’t want to use this I want my code to be perfect and not patched.

the issue was so weird that I was able to see my other UI with in where the image was supposed to be

so hope this helps

and not only for images of CachedNetworkImage I guess this works for the normal non cached ones as well

Thanks for the info, glad to hear I am not the only one with this issue. That sounds like it should fix mine as well. I will give it a try when I get some free time to program again :slight_smile:

1 Like

I havent tried your fix yet. But was experimenting thinking it was a CORS issue. I tried this but it didn’t work for me.

Gave this a try and realized its just for web. I build for Android and iOS. I need to tweak it a little bit but I think I can get it to work after the holidays.

1 Like

Ohh there is an active issue in the cached_network_image

The work around until a fix is very simple you don’t even need this help

Check out their conversation

Thanks so much for sharing this! Life savior!

1 Like