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;
}
}
It supports the web in addition to Android and iOS. Do the following import instead of dart:io : import 'package:universal_io/io. dart';
The dart:io library provides access to files and directories through the File and Directory classes. The following example prints its own source code. To determine the location of the source code being executed, we use the Platform class.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With