无效的浮力扑来导致迟到初始错误

发布于 2025-02-03 08:28:06 字数 521 浏览 1 评论 0原文

我正在构建一个通过蓝牙进行通信的颤音应用程序。当应用程序连接到另一端时,我会在屏幕上几秒钟收到此错误,直到建立连接,然后它消失了,如下所示:

在红色屏幕上:lateinitializationerror:field'连接'初始化。

我认为导致此错误的原因是:

late BluetoothConnection connection ;

但是,如果我删除了迟到,则由于无效安全性,我会遇到错误。我试图禁用整个项目的无效安全性,但错误没有停止。

我还试图使其无效:

BluetoothConnection? connection ;

但是随后我在其他属性中遇到了其他错误:属性“与“连接”无法无条件访问,因为接收器可以是'null'。

  bool get isConnected => connection.isConnected;

I am building a flutter app to communicate via bluetooth. When the app is connecting to the other end I get this error for few seconds on the screen until the connection is established then it goes away whish is like this:

On a red screen: LateInitializationError: Field 'connection' has not been initialized.

I think what is causing this error is this line:

late BluetoothConnection connection ;

But if I remove the late then I get an error because of the null safety. I tried to disable the null safety on the whole project but the error didnt stopp.

I also tried to make it nullable :

BluetoothConnection? connection ;

but then i get other errors in other properties depending on it : The property 'isConnected' can't be unconditionally accessed because the receiver can be 'null'.

  bool get isConnected => connection.isConnected;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神魇的王 2025-02-10 08:28:06

对于那些遇到类似问题的人,这就是我解决的方式:
首先,我将声明表格更改为:

BluetoothConnection? connection ;

  bool isConnecting = true;

 bool get isConnected => (connection?.isConnected ?? false);

  bool isDisconnecting = false;

使BluetoothConnection Nullable Nullable,然后在代码的其余部分添加了一些更改以求解我遇到的错误。
上一个代码:

connection.input?.listen

新代码:

connection!.input!.listen

感谢@jameslin

For those who had similar issue, here is how I solved it:
First I changed the declaration form to:

BluetoothConnection? connection ;

  bool isConnecting = true;

 bool get isConnected => (connection?.isConnected ?? false);

  bool isDisconnecting = false;

To make BluetoothConnection nullable and then I added some changes to the rest of the code to solve the errors I got.
Previous code:

connection.input?.listen

New code:

connection!.input!.listen

Thanks to @jameslin

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文