Flutter - Network image and connection time out

135 views Asked by At

I need network img who have loading progress and error placeholder, and set timeout connection for loading on 5 sec. I tried NetworkImage, FadeInImage and CachedNetworkImage, but i cannot change it anywhere. If my connection is poor, i have loading screen until server get me error, who can leads into infinity loading.

I tried change NetworkImage source code and in there is variable "_sharedHttpClient" where i can add connectionTimeout duration and then its work, but i dont want change source code. Do anyone know some third party library, or simple solution how to solve it? Thanks a lot.

1

There are 1 answers

0
Adnan On

Maybe something like this can help out

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: CachedNetworkImage(
            imageUrl: '', //add imag url here
            placeholder: (context, url) => CircularProgressIndicator(),
            errorWidget: (context, url, error) => Icon(Icons.error),
            cacheManager: DefaultCacheManager(
              httpClient: createHttpClient(), // Custm client method with timeout
            ),
          ),
        ),
      ),
    );
  }

  HttpClient createHttpClient() {
    return HttpClient()..connectionTimeout = Duration(seconds: 5);
  }
}