首页 > 解决方案 > 颤振:[错误:颤振/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]),,),

任何人都可以帮忙!

标签: flutterdartutf-8

解决方案


您正在使用“连接”变量,而没有先初始化或将其分配给某些东西。因此,当调用“connection.output”时,它会导致错误。尝试找到“连接”变量的初始化位置并从那里使用或将其作为函数的参数。这可能有效:

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

推荐阅读