当前位置: 首页 > 知识库问答 >
问题:

Flutter:[ERROR: flutter/lib/ui/ui_dart_state.cc(166)]未处理异常

苍德寿
2023-03-14

我有这个错误,我不明白我的错误在哪里

[错误:flatter/lib/ui/ui\u dart\u state.cc(166)]未处理的异常:NoSuchMethodError:对null调用了getter“output”。E/颤振(16491):接收器:空E/颤振(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]),,),

有人能帮忙吗!

共有1个答案

苏运良
2023-03-14

您正在使用“connection”变量,而没有先初始化或将其分配给某个对象。因此,当被称为“连接”时。输出',它正在导致错误。尝试找到“connection”变量的初始化位置,并从那里使用,或将其作为函数的参数。这可能会奏效:

void _sendOnMessageToBluetooth() async {
    BluetoothConnection connection = new BluetoothConnection();
    connection.output.add(utf8.encode("1" + "\r\n"));
    await connection.output.allSent;
    setState(() {
      deviceState = 1;
    });
  }
 类似资料: