如何修改NetworkImage的Headers?-灵析社区

元气满满才怪啊

不限于某种语言提供给库使用者进行配置的方法(这是我能想到的): 1. 方法的参数中 2. 提供接口 interface 只要实现接口的中方法就可以覆盖默认的方法 3. 继承重写 4. 提供类似AOP的方法 既然想删除一些内容那就继承后实现自己业务,功能和NetWorkImage一样;也就是说里面内容是从flutter3.19.5/packages/flutter/lib/src/painting/_network_image_io.dart这里抄的 import 'dart:async'; import 'dart:io'; import 'dart:ui' as ui; import 'package:flutter/foundation.dart'; typedef _SimpleDecoderCallback = Future Function( ui.ImmutableBuffer buffer); class SteNetworkImage extends ImageProvider implements NetworkImage { /// Creates an object that fetches the image at the given URL. const SteNetworkImage(this.url, {this.scale = 1.0, this.headers}); @override final String url; @override final double scale; @override final Map? headers; @override Future obtainKey(ImageConfiguration configuration) { return SynchronousFuture(this); } @override ImageStreamCompleter loadBuffer( NetworkImage key, DecoderBufferCallback decode) { // Ownership of this controller is handed off to [_loadAsync]; it is that // method's responsibility to close the controller's stream when the image // has been loaded or an error is thrown. final StreamController chunkEvents = StreamController(); return MultiFrameImageStreamCompleter( codec: _loadAsync(key as NetworkImage, chunkEvents, decode: decode), chunkEvents: chunkEvents.stream, scale: key.scale, debugLabel: key.url, informationCollector: () => [ DiagnosticsProperty('Image provider', this), DiagnosticsProperty('Image key', key), ], ); } @override ImageStreamCompleter loadImage( NetworkImage key, ImageDecoderCallback decode) { // Ownership of this controller is handed off to [_loadAsync]; it is that // method's responsibility to close the controller's stream when the image // has been loaded or an error is thrown. final StreamController chunkEvents = StreamController(); return MultiFrameImageStreamCompleter( codec: _loadAsync(key as NetworkImage, chunkEvents, decode: decode), chunkEvents: chunkEvents.stream, scale: key.scale, debugLabel: key.url, informationCollector: () => [ DiagnosticsProperty('Image provider', this), DiagnosticsProperty('Image key', key), ], ); } // Do not access this field directly; use [_httpClient] instead. // We set `autoUncompress` to false to ensure that we can trust the value of // the `Content-Length` HTTP header. We automatically uncompress the content // in our call to [consolidateHttpClientResponseBytes]. static final HttpClient _sharedHttpClient = HttpClient() ..autoUncompress = false; static HttpClient get _httpClient { HttpClient? client; assert(() { if (debugNetworkImageHttpClientProvider != null) { client = debugNetworkImageHttpClientProvider!(); } return true; }()); return client ?? _sharedHttpClient; } Future _loadAsync( NetworkImage key, StreamController chunkEvents, { required _SimpleDecoderCallback decode, }) async { try { assert(key == this); final Uri resolved = Uri.base.resolve(key.url); final HttpClientRequest request = await _httpClient.getUrl(resolved); ///这里实现你的逻辑 headers?.forEach((String name, String value) { request.headers.add(name, value); }); final HttpClientResponse response = await request.close(); if (response.statusCode != HttpStatus.ok) { // The network may be only temporarily unavailable, or the file will be // added on the server later. Avoid having future calls to resolve // fail to check the network again. await response.drain>([]); throw NetworkImageLoadException( statusCode: response.statusCode, uri: resolved); } final Uint8List bytes = await consolidateHttpClientResponseBytes( response, onBytesReceived: (int cumulative, int? total) { chunkEvents.add(ImageChunkEvent( cumulativeBytesLoaded: cumulative, expectedTotalBytes: total, )); }, ); if (bytes.lengthInBytes == 0) { throw Exception('SteNetworkImage is an empty file: $resolved'); } return decode(await ui.ImmutableBuffer.fromUint8List(bytes)); } catch (e) { // Depending on where the exception was thrown, the image cache may not // have had a chance to track the key in the cache at all. // Schedule a microtask to give the cache a chance to add the key. scheduleMicrotask(() { PaintingBinding.instance.imageCache.evict(key); }); rethrow; } finally { chunkEvents.close(); } } @override bool operator ==(Object other) { if (other.runtimeType != runtimeType) { return false; } return other is NetworkImage && other.url == url && other.scale == scale; } @override int get hashCode => Object.hash(url, scale); @override String toString() => '${objectRuntimeType(this, 'SteNetworkImage')}("$url", scale: ${scale.toStringAsFixed(1)})'; }

阅读量:1

点赞量:0

问AI