2

我有一个休息 API,我正在调用它来检索要在我的页面上显示的 PNG 图像。

我的代码:

void getProfilePicture(var pic_id) {

  request = new HttpRequest();
  request.responseType = "blob";
  request.onReadyStateChange.listen(onPicture); 

  // Get Basic Auth credentials
  var authorization = 'Basic '+storage.loginData['password'];

  // Build JSON
  Map reqData = new Map();
  reqData['id'] = pic_id.toString();
  reqData['type'] = 'WEB_SMALL';

  // SEND the request to the server.
  var url = sifted.serverAPI+'/api/v1/pictures/getpicture';
  request.open('POST', url);
  request.withCredentials = false;
  request.setRequestHeader('Authorization',authorization);
  request.setRequestHeader('Content-Type','application/json');
  request.send(json.stringify(reqData));
}

void onPicture(_) {
  if (request.readyState == HttpRequest.DONE &&
      request.status == 200) {
    Blob blob = new Blob(request.response);

    FileReader reader = new FileReader();
    reader.onLoad.listen((fe) { 
      ImageElement imgInput = query('#profilepic');
      imgInput.src = reader.result;
    });
    reader.readAsDataUrl(blob); 
  }  
}

它不起作用,我在 Dart 编辑器中收到这些错误:

Exception: type 'Blob' is not a subtype of type 'List' of 'blobParts'.
Exception: type 'Blob' is not a subtype of type 'List' of 'blobParts'.

关于我做错了什么有什么建议吗?

谢谢 !

4

1 回答 1

3

问题是这一行:

Blob blob = new Blob(request.response);

Blob 构造函数需要 aList而不是另一个Blobrequest.response在您的用例中):factory Blob(List blobParts, [String type, String endings])

只需删除该行,然后直接调用reader.readAsDataUrl(request.response),它应该可以工作。

于 2013-08-17T22:00:59.030 回答