我正在尝试使用协议缓冲区为 Web 和移动设备构建一个跨平台。所以,在我的代码中,我有一个登录功能,我需要导入 grpc_web:
import 'package:flutter/foundation.dart';
import 'package:grpc/grpc.dart';
import 'package:dsu_site/models/user.pb.dart';
import 'package:dsu_site/models/user.pbgrpc.dart' as grpc;
import 'package:grpc/grpc_web.dart' as grpc_web;
import 'package:flutter/services.dart';
import 'dart:convert';
Future<Utilizador> login({String username, String password}) async {
Utilizador user;
if (kIsWeb) {
final channel =
grpc_web.GrpcWebClientChannel.xhr(Uri.parse('https://pl1772:8443'));
final service = grpc.UtilizadoresServiceClient(
channel,
);
var request = Auth();
request.username = username;
request.password = password;
try {
user = await service.login(request);
} catch (e) {
print('Error in login form: $e');
user = grpc.Utilizador();
}
return user;
}
final caCert = await rootBundle.loadString('certs/server.crt');
final ClientChannel channel = ClientChannel(
'localhost',
port: 9002,
options: ChannelOptions(
connectionTimeout: Duration(seconds: 5),
credentials: ChannelCredentials.secure(
authority: 'localhost',
certificates: utf8.encode(caCert),
),
),
);
final service = grpc.UtilizadoresServiceClient(channel);
var request = Auth();
request.username = username;
request.password = password;
try {
user = await service.login(request);
} catch (e) {
print('Error in login form: $e');
user = grpc.Utilizador();
}
return user;
}
问题是当我在网络上运行它(使用 chrome)时它很好,但对于移动设备我得到以下错误:
在 dsu_site 中运行“flutter pub get”...以调试模式在 sdk gphone x86 上启动 lib/main.dart ... lib/main.dart 无效的 depfile:/home/sfm1977/Programming/dsu_site/.dart_tool/flutter_build/efe6e5ca256642d6c0ca767b5f0e6af5 /kernel_snapshot.d 无效的 depfile:/home/sfm1977/Programming/dsu_site/.dart_tool/flutter_build/efe6e5ca256642d6c0ca767b5f0e6af5/kernel_snapshot.d ../../.pub-cache/hosted/pub.dartlang.org/grpc-2.7.0 /lib/src/client/transport/xhr_transport.dart:17:8: 错误: 未找到: 'dart:html' import 'dart:html'; ^ ../../.pub-cache/hosted/pub.dartlang.org/http-0.12.2/lib/src/browser_client.dart:6:8:错误:未找到:'dart:html'导入'飞镖:html'; ^ ../../.pub-cache/hosted/pub.dartlang.org/grpc-2.7.0/lib/src/client/transport/xhr_transport.dart:40:9:错误:找不到类型“HttpRequest” . 最终的 HttpRequest _request;^^^^^^^^^^^ ../../.pub-cache/hosted/pub.dartlang.org/grpc-2.7.0/lib/src/client/transport/xhr_transport.dart:168: 27:错误:找不到类型“HttpRequest”。void _initializeRequest(HttpRequest request, Map<String, String> metadata) { ^^^^^^^^^^^ ../../.pub-cache/hosted/pub.dartlang.org/grpc-2.7.0 /lib/src/client/transport/xhr_transport.dart:181:3:错误:找不到类型“HttpRequest”。HttpRequest createHttpRequest() => HttpRequest(); ^^^^^^^^^^^ ../../.pub-cache/hosted/pub.dartlang.org/http-0.12.2/lib/src/browser_client.dart:34:18:错误: 'HttpRequest' 不是一种类型。最终 _xhrs = {}; ^^^^^^^^^^^ ../../.pub-cache/hosted the fowling /pub.dartlang.org/grpc-2.7.0/lib/src/client/transport/xhr_transport.dart:40 :9: 错误:'HttpRequest' 不是类型。最终的 HttpRequest _request;^^^^^^^^^^^ ../../.pub-cache/hosted/pub.dartlang.org/grpc-2.7.0/lib/src/client/transport/xhr_transport.dart:66: 14:错误:找不到获取器:'HttpRequest'。案例 HttpRequest.HEADERS_RECEIVED: ^^^^^^^^^^^ ../../.pub-cache/hosted/pub.dartlang.org/grpc-2.7.0/lib/src/client/transport/xhr_transport .dart:69:14:错误:找不到吸气剂:'HttpRequest'。case HttpRequest.DONE: ^^^^^^^^^^^ ../../.pub-cache/hosted/pub.dartlang.org/grpc-2.7.0/lib/src/client/transport/xhr_transport .dart:82:30:错误:“ProgressEvent”不是一种类型。_request.onError.listen((ProgressEvent 事件) { ^^^^^^^^^^^^^ ../../.pub-cache/hosted/pub.dartlang.org/grpc-2.7.0/lib /src/client/transport/xhr_transport.dart:168:27: 错误:'HttpRequest' 不是类型。void _initializeRequest(HttpRequest request, Map<String,
- 'XhrClientConnection' 来自'package:grpc/src/client/transport/xhr_transport.dart' ('../../.pub-cache/hosted/pub.dartlang.org/grpc-2.7.0/lib/src /client/transport/xhr_transport.dart')。尝试将名称更正为现有方法的名称,或定义名为“HttpRequest”的方法。HttpRequest createHttpRequest() => HttpRequest(); ^^^^^^^^^^^ ../../.pub-cache/hosted/pub.dartlang.org/grpc-2.7.0/lib/src/client/transport/xhr_transport.dart:200: 11:错误:'HttpRequest' 不是类型。最终的 HttpRequest 请求 = createHttpRequest(); ^^^^^^^^^^^ ../../.pub-cache/hosted/pub.dartlang.org/http-0.12.2/lib/src/browser_client.dart:58:34:错误: 'Blob' 不是一种类型。var blob = xhr.response as Blob ?? 斑点([]);^^^^ ../../.pub-cache/hosted/pub.dartlang.org/http-0.12.2/lib/src/browser_client.dart:46:
- 'BrowserClient' 来自'package:http/src/browser_client.dart' ('../../.pub-cache/hosted/pub.dartlang.org/http-0.12.2/lib/src/browser_client.dart ')。尝试将名称更正为现有方法的名称,或定义名为“HttpRequest”的方法。var xhr = HttpRequest(); ^^^^^^^^^^^ ../../.pub-cache/hosted/pub.dartlang.org/http-0.12.2/lib/src/browser_client.dart:58:42:错误:没有为“BrowserClient”类定义方法“Blob”。
- 'BrowserClient' 来自'package:http/src/browser_client.dart' ('../../.pub-cache/hosted/pub.dartlang.org/http-0.12.2/lib/src/browser_client.dart ')。尝试将名称更正为现有方法的名称,或定义名为“Blob”的方法。var blob = xhr.response as Blob ?? 斑点([]);^^^^ ../../.pub-cache/hosted/pub.dartlang.org/http-0.12.2/lib/src/browser_client.dart:59:20:错误:方法'FileReader'是' t 为“BrowserClient”类定义。
- 'BrowserClient' 来自'package:http/src/browser_client.dart' ('../../.pub-cache/hosted/pub.dartlang.org/http-0.12.2/lib/src/browser_client.dart ')。尝试将名称更正为现有方法的名称,或定义名为“FileReader”的方法。var reader = FileReader(); ^^^^^^^^^^ 未处理的异常:FileSystemException(uri=org-dartlang-untranslatable-uri:dart%3Ahtml; message=StandardFileSystem 仅支持 file:* 和 data:* URI) [38;5;248m# 0 StandardFileSystem.entityForUri (package:front_end/src/api_prototype/standard_file_system.dart:33:7)[39;49m [38;5;248m#1 asFileUri (package:vm/kernel_front_end.dart:599:37)[39; 49m [38;5;248m#2
writeDepfile (package:vm/kernel_front_end.dart:739:21)[39;49m[38;5;244m[39;49m [38;5;248m#3
FrontendCompiler.compile (package:frontend_server/frontend_server.dart:554:15)[39;49m[38;5;244m[39;49m [38;5;248m#4
_FlutterFrontendCompiler.compile (package:flutter_frontend_server/server.dart:43:22)[39;49m[38;5;248m#5 starter (package:flutter_frontend_server/server.dart:180:27)[39;49m [38;5;248m#6 main (file:///b/s/w/ir/cache /builder/src/flutter/flutter_frontend_server/bin/starter.dart:13:30)[39;49m
[38;5;244m#7 _startIsolate。(dart:isolate-patch/isolate_patch.dart:299:32)[39;49m [38;5;244m#8
_RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)[39;49mFAILURE:构建失败并出现异常。
其中:脚本'/home/sfm1977/flutter/packages/flutter_tools/gradle/flutter.gradle'行:900
出了什么问题:任务“:app:compileFlutterBuildDebug”执行失败。
进程 'command'/home/sfm1977/flutter/bin/flutter'' 以非零退出值 1 结束
尝试:使用 --stacktrace 选项运行以获取堆栈跟踪。使用 --info 或 --debug 选项运行以获得更多日志输出。运行 --scan 以获得完整的见解。
在https://help.gradle.org获得更多帮助
10 秒内构建失败异常:Gradle 任务 assembleDebug 失败,退出代码为 1 退出(sigterm)
是否可以同时运行这两个版本,网络版和移动版?