Search code examples
flutterandroid-studiodartbluetoothdata-transfer

The argument type 'List<int>' can't be assigned to the parameter type 'Uint8List'


Im using this code to send data via bluetooth with flutter and i have two questions.

First of all I am getting this error message which I dont know how to fix, the erros is this:

"The argument type 'List' can't be assigned to the parameter type 'Uint8List'".

here is the code:

void _sendMessage(String value1) async {
    value1 = value1.trim();
    if(value1.length >0){
      try{
        connection.output.add(utf8.encode(value1));
        await connection.output.allSent;
      }catch(e){
        setState(() { });


      }
    }
  }

the compiler highlights utf8.encode(value1) as the error

My second question is how i can alter this code to send integers instead of string


Solution

  • somethin like this init

      //send string
      void _sendMessageString(String value1) async {
        value1 = value1.trim();
        if (value1.length > 0) {
          try {
            List<int> list = value1.codeUnits;
            Uint8List bytes = Uint8List.fromList(list);
            connection.output.add(bytes);
            await connection.output.allSent;
          } catch (e) {
            setState(() {});
          }
        }
      }
      
    
      //send int
      void _sendMessageInt(int value1) async {
        try {
          Uint8List bytes = Uint8List(value1);
          connection.output.add(bytes);
          await connection.output.allSent;
        } catch (e) {
          setState(() {});
        }
      }