Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

import 'dart:io'; not support on the web application

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;
  }
}
like image 976
oceany Avatar asked Jul 09 '19 03:07

oceany


People also ask

Is dart IO supported in web?

It supports the web in addition to Android and iOS. Do the following import instead of dart:io : import 'package:universal_io/io. dart';

What is the purpose of dart IO library?

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.


1 Answers

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

like image 87
Suragch Avatar answered Oct 12 '22 03:10

Suragch