Flutter Can';t使用信号器调用方法
我正在创建一个应用程序,并且我使用Signaler,我能够连接到集线器,但每次我遇到此异常时都无法调用方法:Flutter Can';t使用信号器调用方法,flutter,dart,Flutter,Dart,我正在创建一个应用程序,并且我使用Signaler,我能够连接到集线器,但每次我遇到此异常时都无法调用方法:未处理的异常:类型“GeneralError”不是类型转换中类型“Error”的子类型 这是我的代码: var hubConnection; String mobileSession; Future<void> createSignalRConnection() async { hubConnection = new HubConnectionBuilder(
未处理的异常:类型“GeneralError”不是类型转换中类型“Error”的子类型
这是我的代码:
var hubConnection;
String mobileSession;
Future<void> createSignalRConnection() async {
hubConnection =
new HubConnectionBuilder().withUrl(GLOBAL_BACKEND_SESSION_NEW).build();
await hubConnection.start();
logger.log(Level.INFO, "Connection successful");
hubConnection.onclose((error) {
logger.log(Level.INFO, "Connection Closed");
createSignalRConnection();
});
}
Future<bool> sendParingCode(String pairingCode) async {
final result = await hubConnection
.invoke("RequestPairing", args: [mobileSession, pairingCode]);
logger.log(Level.INFO, "Pairing successful");
return true;
}
void initSession() async {
mobileSession = await hubConnection
.invoke("InitSession");
print(mobileSession);
logger.log(Level.INFO, "Session init successful");
}
我认为在调用invoke方法之前,应该等待
createSignalRConnection
完成。试试这个:
@override
void initState() {
super.initState();
_asyncInitState();
}
Future<void> _asyncInitState() async {
await createSignalRConnection(); // HERE: add await
initSession();
}
@覆盖
void initState(){
super.initState();
_AsyncintState();
}
Future\u asyncintstate()异步{
wait createSignalConnection();//此处:添加wait
initSession();
}
您知道如何将其与compute或类似的东西一起使用,以使应用程序不会卡住吗?此代码不应阻塞主事件循环,因为_asyncintstate()在不等待的情况下异步调用。Compute方法产生新的隔离,我认为您不需要在这里这样做。使用主隔离及其事件循环,因为连接和调用操作是轻量级的。顺便说一下,这是一篇有用的文章-
@override
void initState() {
super.initState();
_asyncInitState();
}
Future<void> _asyncInitState() async {
await createSignalRConnection(); // HERE: add await
initSession();
}