Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use the web.Response API.
  • Loading branch information
ditman committed Jul 9, 2024
commit 4ee87cc6816bb33b5013f3e42809cd27ded7f02c
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
// found in the LICENSE file.

import 'dart:convert';
import 'dart:js_interop';
import 'dart:ui_web' as ui_web;

import 'package:flutter/cupertino.dart';
import 'package:http/http.dart' as http;
import 'package:web/web.dart' as html;
import 'package:flutter/widgets.dart';
import 'package:web/web.dart' as web;
import 'package:webview_flutter_platform_interface/webview_flutter_platform_interface.dart';

import 'content_type.dart';
Expand Down Expand Up @@ -39,7 +39,7 @@ class WebWebViewControllerCreationParams

/// The underlying element used as the WebView.
@visibleForTesting
final html.HTMLIFrameElement iFrame = html.HTMLIFrameElement()
final web.HTMLIFrameElement iFrame = web.HTMLIFrameElement()
..id = 'webView${_nextIFrameId++}'
..style.width = '100%'
..style.height = '100%'
Expand Down Expand Up @@ -87,21 +87,21 @@ class WebWebViewController extends PlatformWebViewController {

/// Performs an AJAX request defined by [params].
Future<void> _updateIFrameFromXhr(LoadRequestParams params) async {
final http.Response httpReq =
final web.Response response =
await _webWebViewParams.httpRequestFactory.request(
params.uri.toString(),
method: params.method.serialize(),
requestHeaders: params.headers,
sendData: params.body,
);
) as web.Response;

final String header = httpReq.headers['content-type'] ?? 'text/html';
final String header = response.headers.get('content-type') ?? 'text/html';
final ContentType contentType = ContentType.parse(header);
final Encoding encoding = Encoding.getByName(contentType.charset) ?? utf8;

// ignore: unsafe_html
_webWebViewParams.iFrame.src = Uri.dataFromString(
httpReq.body,
(await response.text().toDart).toDart,
mimeType: contentType.mimeType,
encoding: encoding,
).toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

import 'dart:async';
import 'dart:convert';
import 'dart:js_interop';
import 'dart:ui_web' as ui_web;

import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
import 'package:http/http.dart' as http;
import 'package:web/web.dart' as html;
import 'package:web/web.dart' as web;
// ignore: implementation_imports
import 'package:webview_flutter_platform_interface/src/webview_flutter_platform_interface_legacy.dart';

Expand All @@ -25,7 +25,7 @@ class WebWebViewPlatform implements WebViewPlatform {
WebWebViewPlatform() {
ui_web.platformViewRegistry.registerViewFactory(
'webview-iframe',
(int viewId) => html.HTMLIFrameElement()
(int viewId) => web.HTMLIFrameElement()
..id = 'webview-$viewId'
..width = '100%'
..height = '100%'
Expand All @@ -47,8 +47,8 @@ class WebWebViewPlatform implements WebViewPlatform {
if (onWebViewPlatformCreated == null) {
return;
}
final html.HTMLIFrameElement element = html.document
.getElementById('webview-$viewId')! as html.HTMLIFrameElement;
final web.HTMLIFrameElement element = web.document
.getElementById('webview-$viewId')! as web.HTMLIFrameElement;

final String? initialUrl = creationParams.initialUrl;
if (initialUrl != null) {
Expand All @@ -74,7 +74,7 @@ class WebWebViewPlatformController implements WebViewPlatformController {
/// Constructs a [WebWebViewPlatformController].
WebWebViewPlatformController(this._element);

final html.HTMLIFrameElement _element;
final web.HTMLIFrameElement _element;
HttpRequestFactory _httpRequestFactory = const HttpRequestFactory();

/// Setter for setting the HttpRequestFactory, for testing purposes.
Expand Down Expand Up @@ -203,16 +203,17 @@ class WebWebViewPlatformController implements WebViewPlatformController {
if (!request.uri.hasScheme) {
throw ArgumentError('WebViewRequest#uri is required to have a scheme.');
}
final http.Response httpReq = await _httpRequestFactory.request(
final web.Response response = await _httpRequestFactory.request(
request.uri.toString(),
method: request.method.serialize(),
requestHeaders: request.headers,
sendData: request.body);
sendData: request.body) as web.Response;

final String contentType = httpReq.headers['content-type'] ?? 'text/html';
final String contentType =
response.headers.get('content-type') ?? 'text/html';

_element.src = Uri.dataFromString(
httpReq.body,
(await response.text().toDart).toDart,
mimeType: contentType,
encoding: utf8,
).toString();
Expand Down