1

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.

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.