1
votes

I have this error and i can't understand where is my mistake

[ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: NoSuchMethodError: The getter 'output' was called on null. E/flutter (16491): Receiver: null E/flutter (16491): Tried calling: output

the error refer to this function but i have no error syntax on it

import 'dart:convert';
BluetoothConnection connection;

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

and this is where i'am calling it

FlatButton(
onPressed: _sendOnMessageToBluetooth ==null? "": _sendOnMessageToBluetooth,
child:Text("ON",
style:TextStyle(color:Colors.red[400]),,),

can any one help !

1
What is the connection variable? Where is it initialized in the code? - Jigar Patel
there is BluetoothConnection connection; - Mariam Younes
Edit the question to include that part too. - Jigar Patel
okay i will do that now thanks for your hint - Mariam Younes
So from the code that you have shared so far, it seems like you have only declared the variable connection and not initialized it. Have you assigned an instance of BluetoothConnection to it anywhere in the code? Something like connection = BluetoothConnection() ? - Jigar Patel

1 Answers

2
votes

You are using the 'connection' variable without first initializing or assigning it to something. Hence when called 'connection.output', it is causing an error. Try to find where the 'connection' variable is initialized and use from there or take it as a parameter to function. This might work:

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