我正在用dart / flutter学习流/ bloc模式,无法确定尝试运行应用程序时错误的出处。我认为通过异步错误判断流正在挂断。我正在使用dispose()
并关闭其中的流。任何帮助,将不胜感激。
这是代码。我将所有内容放在一个大块中,但注释了每个代码块来自哪个文件:
// bloc.dart
import 'dart:async';
import 'validators.dart';
class Bloc extends Validators {
final _email = new StreamController<String>();
final _password = new StreamController<String>();
// Add data to stream
Stream<String> get email => _email.stream.transform(validateEmail);
Stream<String> get password => _password.stream.transform(validatePassword);
// Change Data
Function(String) get changeEmail => _email.sink.add;
Function(String) get changePassword => _password.sink.add;
dispose() {
_email.close();
_password.close();
}
}
final bloc = Bloc();
// login_screen.dart
import 'package:flutter/material.dart';
import '../blocs/bloc.dart';
class LoginScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.all(20.0),
child: Column(
children: [
emailField(),
passwordField(),
Container(
margin: EdgeInsets.only(top: 25.0),
),
raisedButton()
],
));
}
Widget emailField() {
return StreamBuilder(
stream: bloc.email,
builder: (context, snapshot) {
return TextField(
onChanged: bloc.changeEmail,
keyboardType: TextInputType.emailAddress,
decoration: InputDecoration(
hintText: 'ardon@exampl.com',
labelText: 'Enter your email!',
errorText: snapshot.error),
);
});
}
Widget passwordField() {
return TextField(
obscureText: true,
decoration: InputDecoration(hintText: 'password', labelText: 'password'),
);
}
Widget raisedButton() {
return RaisedButton(
child: Text('Login'),
color: Colors.blue,
onPressed: () {},
);
}
}
// validation.dart
import 'dart:async';
class Validators {
final validateEmail =
StreamTransformer<String, String>.fromHandlers(handleData: (email, sink) {
if (email.contains('@')) {
sink.add(email);
} else {
sink.addError('Please enter valid email');
}
});
final validatePassword = StreamTransformer<String, String>.fromHandlers(
handleData: (password, sink) {
if (password.length > 4) {
sink.add(password);
} else {
sink.addError('Please enter the right number of characters');
}
});
}
这是我尝试热重启我的应用程序时遇到的错误:
⣯[VERBOSE-2:shell.cc(181)] Dart Error: Can't load Kernel binary: Invalid kernel binary format version.
[VERBOSE-2:engine.cc(158)] Could not prepare to run the isolate.
[VERBOSE-2:engine.cc(117)] Engine not prepare and launch isolate.
Error -32000 received from application: Could not run configuration in engine.
JSON-RPC error -32000: Could not run configuration in engine.
package:json_rpc_2/src/client.dart 110:64 Client.sendRequest
package:json_rpc_2/src/peer.dart 68:15 Peer.sendRequest
package:flutter_tools/src/vmservice.dart 293:13 VMService._sendRequest
package:flutter_tools/src/vmservice.dart 842:12 VM.invokeRpcRaw
===== asynchronous gap ===========================
package:flutter_tools/src/vmservice.dart 865:49 VM.invokeRpc
===== asynchronous gap ===========================
package:flutter_tools/src/vmservice.dart 932:12 VM.runInView
package:flutter_tools/src/vmservice.dart 1439:20 FlutterView.runFromSource
===== asynchronous gap ===========================
package:flutter_tools/src/run_hot.dart 379:18 HotRunner._launchInView
===== asynchronous gap ===========================
package:flutter_tools/src/run_hot.dart 390:13 HotRunner._launchFromDevFS
===== asynchronous gap ===========================
package:flutter_tools/src/run_hot.dart 442:11 HotRunner._restartFromSources
===== asynchronous gap ===========================
package:flutter_tools/src/run_hot.dart 505:15 HotRunner.restart
===== asynchronous gap ===========================
package:flutter_tools/src/run_hot.dart 282:44 HotRunner.handleTerminalCommand
===== asynchronous gap ===========================
package:flutter_tools/src/resident_runner.dart 754:15 ResidentRunner.processTerminalInput
===== asynchronous gap ===========================
dart:async/zone.dart 1132:38 _rootRunUnary
dart:async/zone.dart 1029:19 _CustomZone.runUnary
dart:async/zone.dart 931:7 _CustomZone.runUnaryGuarded
dart:async/stream_impl.dart 336:11 _BufferingStreamSubscription._sendData
dart:async/stream_impl.dart 263:7 _BufferingStreamSubscription._add
dart:async/broadcast_stream_controller.dart 379:20 _SyncBroadcastStreamController._sendData
dart:async/broadcast_stream_controller.dart 254:5 _BroadcastStreamController.add
dart:async/broadcast_stream_controller.dart 480:11 _AsBroadcastStreamController.add
dart:async/zone.dart 1132:38 _rootRunUnary
dart:async/zone.dart 1029:19 _CustomZone.runUnary
dart:async/zone.dart 931:7 _CustomZone.runUnaryGuarded
dart:async/stream_impl.dart 336:11 _BufferingStreamSubscription._sendData
dart:async/stream_impl.dart 263:7 _BufferingStreamSubscription._add
dart:async/stream_transformers.dart 68:11 _SinkTransformerStreamSubscription._add
dart:async/stream_transformers.dart 15:11 _EventSinkWrapper.add
dart:convert/string_conversion.dart 268:11 _StringAdapterSink.add
dart:convert/string_conversion.dart 273:7 _StringAdapterSink.addSlice
dart:convert/string_conversion.dart 348:20 _Utf8ConversionSink.addSlice
dart:convert/ascii.dart 278:17 _ErrorHandlingAsciiDecoderSink.addSlice
dart:convert/ascii.dart 264:5 _ErrorHandlingAsciiDecoderSink.add
dart:convert/chunked_conversion.dart 86:18 _ConverterStreamEventSink.add
dart:async/stream_transformers.dart 120:24 _SinkTransformerStreamSubscription._handleData
dart:async/zone.dart 1132:38 _rootRunUnary
dart:async/zone.dart 1029:19 _CustomZone.runUnary
dart:async/zone.dart 931:7 _CustomZone.runUnaryGuarded
dart:async/stream_impl.dart 336:11 _BufferingStreamSubscription._sendData
dart:async/stream_impl.dart 263:7 _BufferingStreamSubscription._add
dart:async/stream_controller.dart 763:19 _SyncStreamController._sendData
dart:async/stream_controller.dart 639:7 _StreamController._add
dart:async/stream_controller.dart 585:5 _StreamController.add
dart:io/runtime/binsocket_patch.dart 1714:41 _Socket._onData
dart:async/zone.dart 1136:13 _rootRunUnary
dart:async/zone.dart 1029:19 _CustomZone.runUnary
dart:async/zone.dart 931:7 _CustomZone.runUnaryGuarded
dart:async/stream_impl.dart 336:11 _BufferingStreamSubscription._sendData
dart:async/stream_impl.dart 263:7 _BufferingStreamSubscription._add
dart:async/stream_controller.dart 763:19 _SyncStreamController._sendData
dart:async/stream_controller.dart 639:7 _StreamController._add
dart:async/stream_controller.dart 585:5 _StreamController.add
dart:io/runtime/binsocket_patch.dart 1276:33 new _RawSocket.<fn>
dart:io/runtime/binsocket_patch.dart 819:14 _NativeSocket.issueReadEvent.issue
dart:async/schedule_microtask.dart 41:21 _microtaskLoop
dart:async/schedule_microtask.dart 50:5 _startMicrotaskLoop
dart:isolate/runtime/libisolate_patch.dart 115:13 _runPendingImmediateCallback
dart:isolate/runtime/libisolate_patch.dart 172:5 _RawReceivePortImpl._handleMessage
编辑:当我执行热重装而不是热重启时,会出现此错误:
Initializing hot reload... ⣟Hot reload failed: type 'TimeoutException' is not a subtype of type 'Map<String, dynamic>'
#0 HotRunner._reloadSources.<anonymous closure> (package:flutter_tools/src/run_hot.dart:587:45)
#1 _rootRunUnary (dart:async/zone.dart:1132:38)
#2 _CustomZone.runUnary (dart:async/zone.dart:1029:19)
#3 _FutureListener.handleError (dart:async/future_impl.dart:147:20)
#4 Future._propagateToListeners.handleError (dart:async/future_impl.dart:654:47)
#5 Future._propagateToListeners (dart:async/future_impl.dart:675:24)
#6 Future._completeError (dart:async/future_impl.dart:494:5)
#7 Future.wait.handleError (dart:async/future.dart:379:18)
#8 _rootRunBinary (dart:async/zone.dart:1144:38)
#9 _CustomZone.runBinary (dart:async/zone.dart:1037:19)
#10 _FutureListener.handleError (dart:async/future_impl.dart:143:20)
#11 Future._propagateToListeners.handleError (dart:async/future_impl.dart:654:47)
#12 Future._propagateToListeners (dart:async/future_impl.dart:675:24)
#13 Future._completeError (dart:async/future_impl.dart:494:5)
#14 _SyncCompleter._completeError (dart:async/future_impl.dart:55:12)
#15 _Completer.completeError (dart:async/future_impl.dart:27:5)
#16 _AsyncAwaitCompleter.completeError (dart:async/runtime/libasync_patch.dart:40:18)
#17 Isolate.reloadSources (package:flutter_tools/src/vmservice.dart)
<asynchronous suspension>
#18 FlutterDevice.reloadSources (package:flutter_tools/src/resident_runner.dart:144:66)
#19 HotRunner._reloadSources (package:flutter_tools/src/run_hot.dart:579:67)
<asynchronous suspension>
#20 HotRunner.restart (package:flutter_tools/src/run_hot.dart:520:24)
<asynchronous suspension>
#21 HotRunner.handleTerminalCommand (package:flutter_tools/src/run_hot.dart:282:44)
<asynchronous suspension>
#22 ResidentRunner.processTerminalInput (package:flutter_tools/src/resident_runner.dart:754:15)
<asynchronous suspension>
#23 _rootRunUnary (dart:async/zone.dart:1132:38)
#24 _CustomZone.runUnary (dart:async/zone.dart:1029:19)
#25 _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7)
#26 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:336:11)
#27 _BufferingStreamSubscription._add (dart:async/stream_impl.dart:263:7)
#28 _SyncBroadcastStreamController._sendData (dart:async/broadcast_stream_controller.dart:379:20)
#29 _BroadcastStreamController.add (dart:async/broadcast_stream_controller.dart:254:5)
#30 _AsBroadcastStreamController.add (dart:async/broadcast_stream_controller.dart:480:11)
#31 _rootRunUnary (dart:async/zone.dart:1132:38)
#32 _CustomZone.runUnary (dart:async/zone.dart:1029:19)
#33 _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7)
#34 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:336:11)
#35 _BufferingStreamSubscription._add (dart:async/stream_impl.dart:263:7)
#36 _SinkTransformerStreamSubscription._add (dart:async/stream_transformers.dart:68:11)
#37 _EventSinkWrapper.add (dart:async/stream_transformers.dart:15:11)
#38 _StringAdapterSink.add (dart:convert/string_conversion.dart:268:11)
#39 _StringAdapterSink.addSlice (dart:convert/string_conversion.dart:273:7)
#40 _Utf8ConversionSink.addSlice (dart:convert/string_conversion.dart:348:20)
#41 _ErrorHandlingAsciiDecoderSink.addSlice (dart:convert/ascii.dart:278:17)
#42 _ErrorHandlingAsciiDecoderSink.add (dart:convert/ascii.dart:264:5)
#43 _ConverterStreamEventSink.add (dart:convert/chunked_conversion.dart:86:18)
#44 _SinkTransformerStreamSubscription._handleData (dart:async/stream_transformers.dart:120:24)
#45 _rootRunUnary (dart:async/zone.dart:1132:38)
#46 _CustomZone.runUnary (dart:async/zone.dart:1029:19)
#47 _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7)
#48 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:336:11)
#49 _BufferingStreamSubscription._add (dart:async/stream_impl.dart:263:7)
#50 _SyncStreamController._sendData (dart:async/stream_controller.dart:763:19)
#51 _StreamController._add (dart:async/stream_controller.dart:639:7)
#52 _StreamController.add (dart:async/stream_controller.dart:585:5)
#53 _Socket._onData (dart:io/runtime/binsocket_patch.dart:1714:41)
#54 _rootRunUnary (dart:async/zone.dart:1136:13)
#55 _CustomZone.runUnary (dart:async/zone.dart:1029:19)
#56 _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7)
#57 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:336:11)
#58 _BufferingStreamSubscription._add (dart:async/stream_impl.dart:263:7)
#59 _SyncStreamController._sendData (dart:async/stream_controller.dart:763:19)
#60 _StreamController._add (dart:async/stream_controller.dart:639:7)
#61 _StreamController.add (dart:async/stream_controller.dart:585:5)
#62 new _RawSocket.<anonymous closure> (dart:io/runtime/binsocket_patch.dart:1276:33)
#63 _NativeSocket.issueReadEvent.issue (dart:io/runtime/binsocket_patch.dart:819:14)
#64 _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
#65 _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
#66 _runPendingImmediateCallback (dart:isolate/runtime/libisolate_patch.dart:115:13)
#67 _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:172:5)
答案 0 :(得分:1)
一种经常遇到的情况是,您是从源代码构建自己的引擎,并通过flutter的--local-engine
选项使用它。
Flutter应用程序中的Dart代码最初是通过Dart内核编译器编译的,该编译器以中间格式发出.dill
文件。这种中间格式包括标识符,Flutter引擎中的Dart VM使用该标识符来验证其是否与该VM版本兼容。 Invalid kernel binary format version
错误消息的根本原因是所讨论的.dill
文件是使用不兼容的内核编译器版本生成的。
为解决此问题,在为设备重建引擎(例如android_debug_unopt
)之后,您需要确保还重建了关联的主机工具链(例如host_debug_unopt
)。
如果您在其他情况下看到此消息,则几乎可以肯定是一个错误,我绝对鼓励您open an issue对抗Flutter。
答案 1 :(得分:0)
最近修复了一个错误,导致此错误消息https://github.com/flutter/flutter/issues/21348
我希望它会包含在最新的Flutter dev
和master
频道中。
要切换到dev
频道,请使用
flutter channel dev
flutter doctor
更新
(来自https://github.com/flutter/flutter/issues/22964#issuecomment-429071970)
罪魁祸首是import './src/app.dart';
中的 lib/main.dart
。
lib/main.dart
不能包含相对导入,也不能从其他文件导入
https://github.com/dart-lang/sdk#33076 刚刚被修复,但是要等到它落入Flutter为止。