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;
    });
  }
Share:
2,501
Mariam Younes
Author by

Mariam Younes

Updated on November 24, 2022

Comments

  • Mariam Younes
    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
      Jigar Patel over 3 years
      What is the connection variable? Where is it initialized in the code?
    • Mariam Younes
      Mariam Younes over 3 years
      there is BluetoothConnection connection;
    • Jigar Patel
      Jigar Patel over 3 years
      Edit the question to include that part too.
    • Mariam Younes
      Mariam Younes over 3 years
      okay i will do that now thanks for your hint
    • Jigar Patel
      Jigar Patel over 3 years
      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() ?
    • Mariam Younes
      Mariam Younes over 3 years
      no , i'am not do this connection = BluetoothConnection() , where should i add this line and in which is will use ?
    • Jigar Patel
      Jigar Patel over 3 years
      No, 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
      Mariam Younes over 3 years
      can you tell me how to do that because i don't know , please !
    • Jigar Patel
      Jigar Patel over 3 years
      See the usage section on this link - pub.dev/packages/flutter_bluetooth_serial It might help.
    • Mariam Younes
      Mariam Younes over 3 years
      sorry , but i did it before and didn't help me .
  • Mariam Younes
    Mariam Younes over 3 years
    i'm initialize it before using it BluetoothConnection connection;