问题
when I use the dart: io library on the flutter web, a warning appears when debugging .like that
[WARNING]build_web_compilers:entrypoint on web/main.dart: Skipping compiling retgoo_internal|web/main.dart with ddc because some of its transitive libraries have sdk dependencies that not supported on this platform:
retgoo_internal|lib/protocol/http_aiframework.dart
but when I use import 'package: flutter_web / io.dart';
there is an error code, in code .transform
here is the http_aiframework.dart code
import 'dart:async';
import 'dart:convert';
import 'dart:io';
//import 'package:flutter_web/io.dart';
import '../main.dart';
typedef HttpProgressListener(int totalBytes, int receivedBytes, bool done);
class Http {
static String baseURL;
static bool loadAccessToken() {
return accessToken != null;
}
static bool setAccessToken(String token) {
accessToken = token;
return accessToken != null;
}
static bool removeAccessToken() {
accessToken = null;
return accessToken == null;
}
static _processHeader(
{HttpClientRequest request, Map<String, dynamic> headers}) {
if (headers != null) {
headers.forEach((key, value) {
request.headers.add(key, value);
});
}
if (accessToken != null) {
request.headers.set("Authorization", "Bearer " + accessToken);
}
}
static _processResponse(
{HttpClientResponse response,
HttpProgressListener progressListener}) async {
final int totalBytes = response.contentLength;
int receivedBytes = 0;
String body = await response
.transform( //this is the error code when I use import 'package: flutter_web / io.dart';
StreamTransformer.fromHandlers(
handleData: (data, sink) {
sink.add(data);
if (progressListener != null) {
receivedBytes += data.length;
progressListener(totalBytes, receivedBytes, false);
}
},
handleDone: (sink) {
sink.close();
if (progressListener != null) {
progressListener(totalBytes, receivedBytes, true);
}
},
),
)
.map((v) => utf8.decoder.convert(v))
.join();
return body;
}
static getData({
String baseURL,
String endpoint,
Map<String, dynamic> headers,
HttpProgressListener downloadProgressListener,
dynamic data,
}) async {
final client = HttpClient();
client.userAgent = "AIFramework/";
client.connectionTimeout = Duration(seconds: 30);
HttpClientRequest request;
String mBaseURL = baseURL ?? Http.baseURL;
var uri = Uri.parse("$mBaseURL$endpoint");
if (data == null) {
request = await client.getUrl(uri);
} else {
request = await client.postUrl(uri);
}
_processHeader(
request: request,
headers: headers,
);
if (data != null) {
String payload = json.encode(data);
request.write(payload);
}
final response = await request.close();
if (response.statusCode == 200) {
return json.decode(
await _processResponse(
response: response,
progressListener: downloadProgressListener,
),
);
}
return null;
}
}
回答1:
I just ran into this problem myself, looks like the dart:io library won't work with web applications.
From: https://api.flutter.dev/flutter/dart-io/dart-io-library.html
Important: Browser-based applications can't use this library. Only servers, command-line scripts, and Flutter mobile apps can import and use dart:io.
回答2:
If what you want to use from io.dart is WebSocket, you can use HtmlWebSocketChannel from the html package (import 'package:web_socket_channel/html.dart';)
回答3:
You can use the universal_io package. It supports the web in addition to Android and iOS.
dependencies:
universal_io: ^1.0.1
Do the following import instead of dart:io
:
import 'package:universal_io/io.dart';
It works the same way.
Related question: Avoid using web-only libraries outside Flutter web plugin packages
回答4:
StreamTransformer has two generic type, specific them like this
StreamTransformer<List<int>, String>
ps: Dart is a type-safe & dynamic-type language.
来源:https://stackoverflow.com/questions/56945106/import-dartio-not-support-on-the-web-application