hu2ren
hu2ren

Reputation: 149

flutter CachedNetworkImage doesn't work as expected

I'm trying to work with cached images. I followed this https://flutter.dev/docs/cookbook/images/cached-images, but it doesn't work.

CachedNetworkImage throws error: "The argument type 'CircularProgressIndicator' can't be assigned to the parameter type '(BuildContext, String) → Widget'. (argument_type_not_assignable at [hello2] lib/main.dart:21)"

Below is the problem code:

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

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final title = 'Cached Images';

    return MaterialApp(
      title: title,
      home: Scaffold(
        appBar: AppBar(
          title: Text(title),
        ),
        body: Center(
          child: CachedNetworkImage(
            placeholder: CircularProgressIndicator(),
            imageUrl:
            'https://picsum.photos/250?image=9',
          ),
        ),
      ),
    );
  }
}
```dart

Upvotes: 6

Views: 15685

Answers (4)

samad shukr
samad shukr

Reputation: 1049

Reinstall your application and then Run again, It worked for me

CachedNetworkImage(
            placeholder: (context, url) => CircularProgressIndicator(),
            imageUrl: 'https://picsum.photos/250?image=9',
          )

Upvotes: 11

Momin Nurain
Momin Nurain

Reputation: 1

Check the version of the package and its required dart sdk version .

link : https://pub.dev/packages/cached_network_image/versions

Upvotes: 0

krishnaacharyaa
krishnaacharyaa

Reputation: 24940

I would like to add explanation to the above agreed answer.

placeholder is of type Widget Function(BuildContext, String)? placeholder

So it throws you error:

The argument type CircularProgressIndicator can't be assigned to the parameter type (BuildContext, String) → Widget

Convert:

placeholder: CircularProgressIndicator()

to

placeholder: (context, url) => new CircularProgressIndicator(),

Upvotes: 0

hu2ren
hu2ren

Reputation: 149

It works after changing:

placeholder: CircularProgressIndicator(),

to:

placeholder: (context, url) => new CircularProgressIndicator(),

BTW: cached_network_image had a breaking change in 0.6.0.

The tutorial link (https://flutter.dev/docs/cookbook/images/cached-images) is out of date.

Upvotes: 8

Related Questions