Every example I see is nearly identical to the code below. For some reason, this does not work form me. The problem is when I try to assign final MqttPublishMessage message
, it is saying that c[0].payload
is of type 'MqttMessage' which cannot be assigned to a variable of type 'MqttPublishMessage'.
MqttServerClient client = new MqttServerClient(serverUri, clientId);
client.updates.listen((List<MqttReceivedMessage<MqttMessage>> c){
final MqttPublishMessage message = c[0].payload; // Cannot assign type
final payload = MqttPublishPayload.bytesToStringAsString(message.payload.message);
print('Received message:$payload from topic: ${c[0].topic}>');
});
I've tried casting which also doesn't work. I've copied the code exactly and nothing seemed to work. What is going on?
final String payload = MqttPublishPayload.bytesToStringAsString(message.payload.message);
print('Received message:$payload from topic: <${c[0].topic}>');