gpt4 book ai didi

android - Flutter Event Channel Stream 导致应用程序崩溃(与设备的连接丢失)

转载 作者:行者123 更新时间:2023-12-03 03:58:16 27 4
gpt4 key购买 nike

我正在按照本 article 中描述的教程进行操作。 .
文章代码可以在这里找到:https://github.com/seamusv/event_channel_sample .
我基本上做同样的事情,只是我使用 kotlin 而不是 java。

在 native 代码 (MainActivity.kt) 中:

class MainActivity: FlutterActivity() {
private val STREAM_TAG = "alarm.eventchannel.sample/stream";

private var timerSubscription : Disposable? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
GeneratedPluginRegistrant.registerWith(this)

EventChannel(getFlutterView(), STREAM_TAG).setStreamHandler(
object : EventChannel.StreamHandler {
override fun onListen(arguments: Any?, events: EventChannel.EventSink) {
Log.w("TAG", "adding listener")
this@MainActivity.timerSubscription = Observable.interval(0, 1, TimeUnit.SECONDS)
.subscribe (
{
Log.w("Test", "Result we just received: $it");
events.success(1);
}, // OnSuccess
{ error -> events.error("STREAM", "Error in processing observable", error); }, // OnError
{ println("Complete"); } // OnCompletion
)
}

override fun onCancel(arguments: Any?) {
Log.w("TAG", "adding listener")
if (this@MainActivity.timerSubscription != null) {
this@MainActivity.timerSubscription?.dispose()
this@MainActivity.timerSubscription = null
}
}
}
)

}
}

在我的 main.dart 中,我执行以下操作:
int _timer = 0;
StreamSubscription _timerSubscription = null;

void _enableTimer() {
if (_timerSubscription == null) {
_timerSubscription = stream.receiveBroadcastStream().listen(_updateTimer);
}
}

void _disableTimer() {
if (_timerSubscription != null) {
_timerSubscription.cancel();
_timerSubscription = null;
}
}

void _updateTimer(timer) {
debugPrint("Timer $timer");
setState(() => _timer = timer);
}

在构建函数中,我还创建了一个按钮,然后调用 _enableTimer() onPressed。
new FlatButton(
child: const Text('Enable'),
onPressed: _enableTimer,
)

每当我现在按下按钮调用 _enableTimer() 时,应用程序就会崩溃,并且我会得到输出“与设备的连接丢失”...
我做错了什么还是因为这篇文章来自 2017 年 12 月,所以这是更新版本的 Flutter 中的错误?

最佳答案

我的问题的解决方案基本上是在主线程中启动流:

class MainActivity: FlutterActivity() {
private val CHANNEL = "alarm.flutter.dev/audio"

private val STREAM_TAG = "alarm.eventchannel.sample/stream";

private var timerSubscription : Disposable? = null

override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine)

EventChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), STREAM_TAG).setStreamHandler(
object : EventChannel.StreamHandler {
override fun onListen(arguments: Any?, events: EventChannel.EventSink) {
Log.w("TAG", "adding listener")
this@MainActivity.timerSubscription = Observable
.interval(1000, TimeUnit.MILLISECONDS)
.observeOn(AndroidSchedulers.mainThread())
.subscribe (
{
Log.w("Test", "Result we just received: $it");
events.success(it);
}, // OnSuccess
{ error -> events.error("STREAM", "Error in processing observable", error); }, // OnError
{ println("Complete"); } // OnCompletion
)
}

override fun onCancel(arguments: Any?) {
Log.w("TAG", "adding listener")
if (this@MainActivity.timerSubscription != null) {
this@MainActivity.timerSubscription?.dispose()
this@MainActivity.timerSubscription = null
}
}
}
)

}

关于android - Flutter Event Channel Stream 导致应用程序崩溃(与设备的连接丢失),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59445248/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com