image:flutter网络图片

  1. 1. 概述
  2. 2. 代码模板
  3. 3. 未认证的证书

1. 概述

Image是flutter的图片类,支持本地图片,网络图片。
example代码位置: example

2. 代码模板

import 'package:flutter/material.dart';
import 'package:flutter_example/resources/image_keys.dart';

class MyNetworkImage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Card(
      child: Column(
        children: <Widget>[
          ListTile(
            title: Text('Network Image'),
          ),
          Divider(),
          Image.network(ImageKeys.avatar),
        ],
      ),
    );
  }
}

3. 未认证的证书

由于是网络图片,所以当进行访问未认证的https服务器(本地调试)时, 会遇到问题,因此需要跳过证书认证。

Network Image - Connection closed before full header was received…

flutter: When the exception was thrown, this was the stack:
flutter: #0      NetworkImage._loadAsync (package:flutter/src/painting/image_provider.dart:490:41)
flutter: <asynchronous suspension>
flutter: #1      NetworkImage.load (package:flutter/src/painting/image_provider.dart:471:14)
flutter: #2      ImageProvider.resolve.<anonymous closure>.<anonymous closure> (package:flutter/src/painting/image_provider.dart:267:86)
flutter: #3      ImageCache.putIfAbsent (package:flutter/src/painting/image_cache.dart:143:20)
flutter: #4      ImageProvider.resolve.<anonymous closure> (package:flutter/src/painting/image_provider.dart:267:63)
flutter: #5      SynchronousFuture.then (package:flutter/src/foundation/synchronous_future.dart:38:29)
flutter: #6      ImageProvider.resolve (package:flutter/src/painting/image_provider.dart:265:30)
flutter: #7      _ImageState._resolveImage (package:flutter/src/widgets/image.dart:630:20)
flutter: #8      _ImageState.didChangeDependencies (package:flutter/src/widgets/image.dart:605:5)
flutter: #9      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3846:12)
flutter: #10     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3696:5)
flutter: #11     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
flutter: #12     Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)

使用HttpOverrides跳过证书认证。example代码位置: example

import 'dart:io';  // ☆☆☆☆☆
import 'package:flutter/material.dart';
import 'package:fluro/fluro.dart';
import 'package:flutter_example/routers/routers.dart';
import 'package:flutter_example/routers/application.dart';

class MyHttpOverrides extends HttpOverrides{
  @override
  HttpClient createHttpClient(SecurityContext context){
    return super.createHttpClient(context)
      ..badCertificateCallback = (X509Certificate cert, String host, int port)=> true;
  }
}

void main() {
  HttpOverrides.global = MyHttpOverrides();  // ☆☆☆☆☆
  runApp(MyApp());
}

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 wind.kaisa@gmail.com

文章标题:image:flutter网络图片

本文作者:kaisawind

发布时间:2019-09-21, 08:09:21

最后更新:2020-08-10, 08:17:32

原始链接:https://kaisawind.gitee.io/2019/09/21/2019-09-21-flutter-image/

版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。

目录
×

喜欢就点赞,疼爱就打赏