Home > Software engineering >  CachedNetworkImage in flutter does not work on local images
CachedNetworkImage in flutter does not work on local images

Time:09-10

This maybe a stupid question given the name but does CachedNetworkImage works on local images?

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

class SigninPage extends StatelessWidget {
  const SigninPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text("Welcome"),
        ),
        body: 
            Center(
              child: CachedNetworkImage(
                    imageUrl: 'assets/images/apple_cartoon.png',
                    imageBuilder: (context, imageProvider) {
                      return Container(
                          decoration: BoxDecoration(
                              image: DecorationImage(
                                  image: imageProvider, fit: BoxFit.cover)));
                    },
                    placeholder: (context, url) =>
                        const CircularProgressIndicator(),
                    errorWidget: (context, url, error) => const Icon(Icons.error),
                  ),
            ),
        );
  }
}

This returns the error icon

Using Image.asset instead works fine, so I assume that you cannot use local images with CachedNetworkImage but couldn't find any confirmation online and want to be sure this was the case.

CodePudding user response:

CachedNetworkImage is used to retrieve an image from the internet to store it in a cache and then be able to redisplay it even if you have no connection. While your local image doesn't need a connection to be displayed. That's why you can only give a url in the imageUrl field of the Widget

CodePudding user response:

CachedNetworkImage is a flutter library to show images from the internet and keep them in the cache directory.

  • Related