I'm trying to build a cross platform in flutter for web and mobile with protocol buffers. So, in my code i have a function to login and I need to import 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;
}
The problem is when I run this for the web (using chrome) it's fine but for mobile, i get the following errors:
Running "flutter pub get" in dsu_site... Launching lib/main.dart on sdk gphone x86 in debug mode... lib/main.dart Invalid depfile: /home/sfm1977/Programming/dsu_site/.dart_tool/flutter_build/efe6e5ca256642d6c0ca767b5f0e6af5/kernel_snapshot.d Invalid 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: Error: Not found: 'dart:html' import 'dart:html'; ^ ../../.pub-cache/hosted/pub.dartlang.org/http-0.12.2/lib/src/browser_client.dart:6:8: Error: Not found: 'dart:html' import 'dart:html'; ^ ../../.pub-cache/hosted/pub.dartlang.org/grpc-2.7.0/lib/src/client/transport/xhr_transport.dart:40:9: Error: Type 'HttpRequest' not found. final HttpRequest _request; ^^^^^^^^^^^ ../../.pub-cache/hosted/pub.dartlang.org/grpc-2.7.0/lib/src/client/transport/xhr_transport.dart:168:27: Error: Type 'HttpRequest' not found. 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: Error: Type 'HttpRequest' not found. HttpRequest createHttpRequest() => HttpRequest(); ^^^^^^^^^^^ ../../.pub-cache/hosted/pub.dartlang.org/http-0.12.2/lib/src/browser_client.dart:34:18: Error: 'HttpRequest' isn't a type. final _xhrs = {}; ^^^^^^^^^^^ ../../.pub-cache/hostedthe fowling /pub.dartlang.org/grpc-2.7.0/lib/src/client/transport/xhr_transport.dart:40:9: Error: 'HttpRequest' isn't a type. final HttpRequest _request; ^^^^^^^^^^^ ../../.pub-cache/hosted/pub.dartlang.org/grpc-2.7.0/lib/src/client/transport/xhr_transport.dart:66:14: Error: Getter not found: 'HttpRequest'. case HttpRequest.HEADERS_RECEIVED: ^^^^^^^^^^^ ../../.pub-cache/hosted/pub.dartlang.org/grpc-2.7.0/lib/src/client/transport/xhr_transport.dart:69:14: Error: Getter not found: 'HttpRequest'. case HttpRequest.DONE: ^^^^^^^^^^^ ../../.pub-cache/hosted/pub.dartlang.org/grpc-2.7.0/lib/src/client/transport/xhr_transport.dart:82:30: Error: 'ProgressEvent' isn't a type. _request.onError.listen((ProgressEvent event) { ^^^^^^^^^^^^^ ../../.pub-cache/hosted/pub.dartlang.org/grpc-2.7.0/lib/src/client/transport/xhr_transport.dart:168:27: Error: 'HttpRequest' isn't a type. 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:38: Error: The method 'HttpRequest' isn't defined for the class 'XhrClientConnection'.
- 'XhrClientConnection' is from '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'). Try correcting the name to the name of an existing method, or defining a method named 'HttpRequest'. HttpRequest createHttpRequest() => HttpRequest(); ^^^^^^^^^^^ ../../.pub-cache/hosted/pub.dartlang.org/grpc-2.7.0/lib/src/client/transport/xhr_transport.dart:200:11: Error: 'HttpRequest' isn't a type. final HttpRequest request = createHttpRequest(); ^^^^^^^^^^^ ../../.pub-cache/hosted/pub.dartlang.org/http-0.12.2/lib/src/browser_client.dart:58:34: Error: 'Blob' isn't a type. var blob = xhr.response as Blob ?? Blob([]); ^^^^ ../../.pub-cache/hosted/pub.dartlang.org/http-0.12.2/lib/src/browser_client.dart:46:15: Error: The method 'HttpRequest' isn't defined for the class 'BrowserClient'.
- 'BrowserClient' is from 'package:http/src/browser_client.dart' ('../../.pub-cache/hosted/pub.dartlang.org/http-0.12.2/lib/src/browser_client.dart'). Try correcting the name to the name of an existing method, or defining a method named 'HttpRequest'. var xhr = HttpRequest(); ^^^^^^^^^^^ ../../.pub-cache/hosted/pub.dartlang.org/http-0.12.2/lib/src/browser_client.dart:58:42: Error: The method 'Blob' isn't defined for the class 'BrowserClient'.
- 'BrowserClient' is from 'package:http/src/browser_client.dart' ('../../.pub-cache/hosted/pub.dartlang.org/http-0.12.2/lib/src/browser_client.dart'). Try correcting the name to the name of an existing method, or defining a method named 'Blob'. var blob = xhr.response as Blob ?? Blob([]); ^^^^ ../../.pub-cache/hosted/pub.dartlang.org/http-0.12.2/lib/src/browser_client.dart:59:20: Error: The method 'FileReader' isn't defined for the class 'BrowserClient'.
- 'BrowserClient' is from 'package:http/src/browser_client.dart' ('../../.pub-cache/hosted/pub.dartlang.org/http-0.12.2/lib/src/browser_client.dart'). Try correcting the name to the name of an existing method, or defining a method named 'FileReader'. var reader = FileReader(); ^^^^^^^^^^ Unhandled exception: FileSystemException(uri=org-dartlang-untranslatable-uri:dart%3Ahtml; message=StandardFileSystem only supports file:* and data:* URIs) [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: Build failed with an exception.
Where: Script '/home/sfm1977/flutter/packages/flutter_tools/gradle/flutter.gradle' line: 900
What went wrong: Execution failed for task ':app:compileFlutterBuildDebug'.
Process 'command '/home/sfm1977/flutter/bin/flutter'' finished with non-zero exit value 1
Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
Get more help at https://help.gradle.org
BUILD FAILED in 10s Exception: Gradle task assembleDebug failed with exit code 1 Exited (sigterm)
Is it possible to run the two releases together, web and mobile?