如何解决CachedNetworkImage中的错误?

时间:2018-11-21 07:59:12

标签: dart flutter

我想加载要缓存的图像。所以我为此使用了CachedNetworkImage。当用户通过gmail帐户登录时,我会获得图像网址并显示图像。但是我需要将其保留在缓存中。 这是我的代码:

new Center(
 child: new Column(
  children: <Widget>[
   new CircleAvatar(
    new CachedNetworkImage(
      placeholder: CircularProgressIndicator(),
      imageUrl: widget.currentUser?.profilUrl,
    ),
  ),
],
),
)

我也使用了CachedNetworkImageProvider,但是两者都出现相同的错误。错误是

type'CachedNetworkImage'is not  a  subtype  of  type  'ImageProvider<dynamic>'

3 个答案:

答案 0 :(得分:1)

小部件CircleAvatar收到一个ImageProvider

cached_network_image软件包为您提供了两个要使用的类:

  1. CachedNetworkImage一个小部件,可用于显示缓存的网络图像。
  2. CachedNetworkImageProvider一个提供缓存图像的 ImageProvider

因此,如果要将其传递给CachedNetworkImageProvider,则必须使用CircleAvatar(2。)。

这是一个完整示例,您可以复制并粘贴以进行尝试:

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) {
    return MaterialApp(
      theme: ThemeData(),
      home: Scaffold(
        body: Center(
          child: CircleAvatar(
            backgroundImage: CachedNetworkImageProvider(
              'https://upload.wikimedia.org/wikipedia/commons/thumb/7/71/Bill_Gates_Buys_Skype_%285707954468%29.jpg/2560px-Bill_Gates_Buys_Skype_%285707954468%29.jpg'
            ),
          ),
        ),
      )
    );
  }
}

答案 1 :(得分:1)

CircleAvatar def performBatchDetectV2(image_list, thresh= 0.25, configPath = "./cfg/yolov4.cfg", weightPath = "yolov4.weights", metaPath= "./cfg/coco.data", hier_thresh=.5, nms=.45, batch_size=3): net = load_net_custom(configPath.encode('utf-8'), weightPath.encode('utf-8'), 0, batch_size) meta = load_meta(metaPath.encode('utf-8')) pred_height, pred_width, c = image_list[0].shape net_width, net_height = (network_width(net), network_height(net)) img_list = [] for custom_image_bgr in image_list: custom_image = cv2.cvtColor(custom_image_bgr, cv2.COLOR_BGR2RGB) custom_image = cv2.resize( custom_image, (net_width, net_height), interpolation=cv2.INTER_NEAREST) custom_image = custom_image.transpose(2, 0, 1) img_list.append(custom_image) arr = np.concatenate(img_list, axis=0) arr = np.ascontiguousarray(arr.flat, dtype=np.float32) / 255.0 data = arr.ctypes.data_as(POINTER(c_float)) im = IMAGE(net_width, net_height, c, data) batch_dets = network_predict_batch(net, im, batch_size, pred_width, pred_height, thresh, hier_thresh, None, 0, 0) batch_boxes = [] batch_scores = [] batch_classes = [] for b in range(batch_size): num = batch_dets[b].num dets = batch_dets[b].dets if nms: do_nms_obj(dets, num, meta.classes, nms) boxes = [] scores = [] classes = [] for i in range(num): det = dets[i] score = -1 label = None for c in range(det.classes): p = det.prob[c] if p > score: score = p label = c if score > thresh: box = det.bbox left, top, right, bottom = map(int,(box.x - box.w / 2, box.y - box.h / 2, box.x + box.w / 2, box.y + box.h / 2)) boxes.append((top, left, bottom, right)) scores.append(score) classes.append(label) # boxColor = (int(255 * (1 - (score ** 2))), int(255 * (score ** 2)), 0) # cv2.rectangle(image_list[b], (left, top), # (right, bottom), boxColor, 2) # cv2.imwrite(os.path.basename(img_samples[b]),image_list[b]) batch_boxes.append(boxes) batch_scores.append(scores) batch_classes.append(classes) free_batch_detections(batch_dets, batch_size) return batch_boxes, batch_scores, batch_classes 属性需要ImageProvider,而不是Image小部件。

ImageProviders :-NetworkImage(),AssetImage()和CachedNetworkImageProvider()。

图像小部件:-Image(),CircleAvatar(),CachedNetworkImage()

因此CircleAvatar()和CachedNetworkImage()也是与我们通常使用的与Image()小部件相同的图像显示小部件。

每个显示图像的小部件都需要图像提供程序。 因此,在这种情况下,您必须将CircleAvatar()小部件与CachedNetworkImageProvider()一起使用,因为此CachedNetworkImageProvider()提供CircleAvatar()小部件所需的图像数据。

答案 2 :(得分:0)

就像@Niklas所说的。

  

小部件CircleAvatar收到一个ImageProvider。

这是使用方法:

 new Center(
  child: new Column(
   children: <Widget>[
    new CircleAvatar(
     backgroundImage: new CachedNetworkImageProvider(
      widget.currentUser?.profilUrl,
     )
    ),
   ],
  ),
 )