flutter - 颤振:[错误:颤振/lib/ui/ui_dart_state.cc(166)]未处理的异常
问题描述
我有这个错误,我不明白我的错误在哪里
[错误:flutter/lib/ui/ui_dart_state.cc(166)] 未处理的异常:NoSuchMethodError:在 null 上调用了 getter '输出'。E/flutter(16491):接收者:空 E/flutter(16491):尝试调用:输出
错误是指这个函数,但我没有错误语法
import 'dart:convert';
BluetoothConnection connection;
void _sendOnMessageToBluetooth() async {
connection.output.add(utf8.encode("1" + "\r\n"));
await connection.output.allSent;
setState(() {
deviceState = 1;
});
}
这就是我所说的
FlatButton(
onPressed: _sendOnMessageToBluetooth ==null? "": _sendOnMessageToBluetooth,
child:Text("ON",
style:TextStyle(color:Colors.red[400]),,),
任何人都可以帮忙!
解决方案
您正在使用“连接”变量,而没有先初始化或将其分配给某些东西。因此,当调用“connection.output”时,它会导致错误。尝试找到“连接”变量的初始化位置并从那里使用或将其作为函数的参数。这可能有效:
void _sendOnMessageToBluetooth() async {
BluetoothConnection connection = new BluetoothConnection();
connection.output.add(utf8.encode("1" + "\r\n"));
await connection.output.allSent;
setState(() {
deviceState = 1;
});
}
推荐阅读
- bash - 使用 bash 脚本迭代地运行带有参数的 C++ 程序
- java - 实现 onSaveInstanceState 并自定义它以在旋转屏幕后保存文本后应用程序崩溃
- c# - 链接实体在 Core 中编辑时忘记其 ID 导致错误 - 需要保存在不可见的控制器中
- javascript - Javascript 如何处理导出的模块级变量?
- r - 试图在 R 中传播数据帧
- angularjs - 从导入的模型中调用时,createInput() 的行为不符合预期
- r - 使用 zoo 对象创建多个滞后变量
- maven - mvn verify sonar:sonar 命令是否将代码推送到声纳 Web 仪表板?
- autohotkey - 使用 AutoHotKey 暂时禁用卡住的修饰键
- javascript - 如何在 TypeScript 中使用无 typescript React 组件?