Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get 请求怎么读取图片 #210

Closed
9-lives opened this issue Mar 7, 2019 · 3 comments
Closed

get 请求怎么读取图片 #210

9-lives opened this issue Mar 7, 2019 · 3 comments

Comments

@9-lives
Copy link

9-lives commented Mar 7, 2019

尝试 get 请求图片。

  1. 如果使用泛型,报错;
  2. 如果不使用泛型,rs 是 ResponseBody 类型,要怎么处理才能完成 flutter 显示图片?

Steps to Reproduce

使用泛型

  testF () async {
    /// 下面一行的 Response、get后面有泛型HttpClientResponse,不知道为什么在issue里没显示
    Response<HttpClientResponse> rs = await Dio().get<HttpClientResponse>(
      'https://admin-test.shiguangkey.com/captcha.jpg',
      options: Options(
        responseType: ResponseType.stream,
      ),
    );
    print('-------result');
    print(rs);
  }

Logs

type 'ResponseBody' is not a subtype of type 'HttpClientResponse'

@wendux
Copy link
Contributor

wendux commented Mar 8, 2019

2.x中ResponseType.stream对应的返回类型为ResponseBody, ResponseBody.stream即为响应流。

    Response<ResponseBody> rs = await Dio().get<ResponseBody>(
      'https://admin-test.shiguangkey.com/captcha.jpg',
      options: Options(
        responseType: ResponseType.stream,
      ),
    );
    print(rs.data.stream );

另外,你也可以通过设置ResponseType.bytes来直接接受图片内容,结果为二进制数组:

    Response<List<int>> rs = await Dio().get<List<int>>(
      'https://admin-test.shiguangkey.com/captcha.jpg',
      options: Options(
        responseType: ResponseType.bytes,
      ),
    );
    print(rs.data);

@9-lives
Copy link
Author

9-lives commented Mar 8, 2019

谢谢!建议更新 readme 文档。

@AlexV525
Copy link
Member

@wendux 你好!目前有一个需求是使用dio来获取一张图片,该图片资源在https下,由于根证书的问题需要跳过https校验。如何在利用dio跳过证书校验的同时,将response转为ImageProvider或者Image利用getImage代替NetworkImage)?

部分代码如下:

image: new DecorationImage(
    image: new NetworkImage(thumbImgUrl),
    fit: BoxFit.cover
)
static Future<Stream> getImage(String url) async {
    (dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate  = (client) {
        client.badCertificateCallback =
            (X509Certificate cert, String host, int port) => true;
    };
    Response response = await dio.get(
        url,
        options: Options(responseType: ResponseType.stream)
    );
    return response.data.stream;
}

@wendux wendux closed this as completed Apr 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants