导入'dart:io'; Web应用程序不支持

时间:2019-07-09 03:54:00

标签: flutter-dependencies flutter-web

当我在Flutter网站上使用dart:io库时,在调试.like时会出现警告

  

[WARNING] build_web_compilers:web / main.dart上的入口点:使用ddc跳过编译retgoo_internal | web / main.dart,因为其中有些   传递库具有sdk依赖关系,对此不支持   平台:

     

retgoo_internal | lib / protocol / http_aiframework.dart

但是当我使用import 'package: flutter_web / io.dart';时,出现错误代码,代码为.transform 这是http_aiframework.dart代码

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;
  }
}

4 个答案:

答案 0 :(得分:4)

您可以使用universal_io软件包。除了Android和iOS,它还支持网络。

dependencies:
  universal_io: ^1.0.1

执行以下导入,而不是dart:io

import 'package:universal_io/io.dart';

工作原理相同。

相关问题:Avoid using web-only libraries outside Flutter web plugin packages

答案 1 :(得分:2)

我本人只是遇到了这个问题,看起来dart:io库不适用于Web应用程序。

发件人:https://api.flutter.dev/flutter/dart-io/dart-io-library.html

重要提示:基于浏览器的应用程序无法使用此库。只有服务器,命令行脚本和Flutter移动应用程序才能导入和使用dart:io。

答案 2 :(得分:0)

StreamTransformer具有两种泛型类型,具体来说就是这样

StreamTransformer ,字符串>

ps:Dart是一种类型安全和动态类型的语言。

答案 3 :(得分:0)

如果要从io.dart使用的是WebSocket,则可以使用html包中的HtmlWebSocketChannel(导入“ package:web_socket_channel / html.dart”;)