Home > Mobile >  How can i catch exception when loading the image in the NetworkImage?
How can i catch exception when loading the image in the NetworkImage?

Time:08-22

I'm trying to lose connection for testing reason, but while i'm doing this an error occured

======== Exception caught by image resource service ================================================
The following HttpException was thrown resolving an image codec:
Software caused connection abort, uri = 
https://firebasestorage.googleapis.com/v0/Cover Pictures/2022-08-22 01:08:40.718456?alt=media&token=8b9c387e-c1fe-4cb7-843a-370d6cbfb43f

When the exception was thrown, this was the stack: 
Image provider: NetworkImage("https://firebasestorage.googleapis.com/v0/b

Code Below:

Container(
      decoration: BoxDecoration(
        color: Colors.grey,
        borderRadius: BorderRadius.all(Radius.circular(1.h)),
        image: DecorationImage(
          fit: BoxFit.cover,
          image: _coverImage != null
              ? FileImage(File(_coverImage!.path)) as ImageProvider
              : NetworkImage(authProvider.coverImageUrl!),
        ),
      ),
    ),

CodePudding user response:

use this packeg cached_network_image

CachedNetworkImage(
    imageUrl: "http://via.placeholder.com/350x150",
    placeholder: (context, url) => CircularProgressIndicator(),
    errorWidget: (context, url, error) => Icon(Icons.error),
 ),

CodePudding user response:

Use Image.network errorBuilder property

Image.network('http://rtablada.github.io/ember-profile-upload/profile.jpg',
  errorBuilder: (BuildContext context, Object exception, StackTrace stackTrace) {
      return Text('Your error widget...');
  },
),
  • Related