Flutter :[ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception
2,501
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;
});
}
Author by
Mariam Younes
Updated on November 24, 2022Comments
-
Mariam Younes over 1 year
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 !
-
Jigar Patel over 3 yearsWhat is the
connection
variable? Where is it initialized in the code? -
Mariam Younes over 3 yearsthere is BluetoothConnection connection;
-
Jigar Patel over 3 yearsEdit the question to include that part too.
-
Mariam Younes over 3 yearsokay i will do that now thanks for your hint
-
Jigar Patel over 3 yearsSo 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 ofBluetoothConnection
to it anywhere in the code? Something likeconnection = BluetoothConnection()
? -
Mariam Younes over 3 yearsno , i'am not do this connection = BluetoothConnection() , where should i add this line and in which is will use ?
-
Jigar Patel over 3 yearsNo, you should not add this exact line. It was only for example. The point is that you need to assign an instance of
BluetoothConnection
to it before you can call methods/getters on it. -
Mariam Younes over 3 yearscan you tell me how to do that because i don't know , please !
-
Jigar Patel over 3 yearsSee the usage section on this link - pub.dev/packages/flutter_bluetooth_serial It might help.
-
Mariam Younes over 3 yearssorry , but i did it before and didn't help me .
-
-
Mariam Younes over 3 yearsi'm initialize it before using it BluetoothConnection connection;