'MqttMessage' 类型的值不能分配给 'MqttPublishMessage' 类型的变量
A value of type 'MqttMessage' can't be assigned to a variable of type 'MqttPublishMessage'
我看到的每个示例都与下面的代码几乎相同。出于某种原因,这对我不起作用。问题是当我尝试分配 final MqttPublishMessage message
时,它说 c[0].payload
是 'MqttMessage' 类型,不能分配给 'MqttPublishMessage'.[=15= 类型的变量]
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}>');
});
我试过强制转换,但还是不行。我已经准确地复制了代码,但似乎没有任何效果。怎么回事?
这是提供的完整示例 here
client.updates!.listen((List<MqttReceivedMessage<MqttMessage?>>? c) {
final recMess = c![0].payload as MqttPublishMessage;
final pt =
MqttPublishPayload.bytesToStringAsString(recMess.payload.message!);
/// The above may seem a little convoluted for users only interested in the
/// payload, some users however may be interested in the received publish message,
/// lets not constrain ourselves yet until the package has been in the wild
/// for a while.
/// The payload is a byte buffer, this will be specific to the topic
print(
'EXAMPLE::Change notification:: topic is <${c[0].topic}>, payload is <-- $pt -->');
print('');
});
如您所见,他正在创建没有类型的变量,并且正在重铸:
final recMess = c![0].payload as MqttPublishMessage;
如果这不起作用,我会在客户端设置中查找一些错误,或者您收到的数据可能与您期望收到的不完全一致。
我看到的每个示例都与下面的代码几乎相同。出于某种原因,这对我不起作用。问题是当我尝试分配 final MqttPublishMessage message
时,它说 c[0].payload
是 'MqttMessage' 类型,不能分配给 'MqttPublishMessage'.[=15= 类型的变量]
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}>');
});
我试过强制转换,但还是不行。我已经准确地复制了代码,但似乎没有任何效果。怎么回事?
这是提供的完整示例 here
client.updates!.listen((List<MqttReceivedMessage<MqttMessage?>>? c) {
final recMess = c![0].payload as MqttPublishMessage;
final pt =
MqttPublishPayload.bytesToStringAsString(recMess.payload.message!);
/// The above may seem a little convoluted for users only interested in the
/// payload, some users however may be interested in the received publish message,
/// lets not constrain ourselves yet until the package has been in the wild
/// for a while.
/// The payload is a byte buffer, this will be specific to the topic
print(
'EXAMPLE::Change notification:: topic is <${c[0].topic}>, payload is <-- $pt -->');
print('');
});
如您所见,他正在创建没有类型的变量,并且正在重铸:
final recMess = c![0].payload as MqttPublishMessage;
如果这不起作用,我会在客户端设置中查找一些错误,或者您收到的数据可能与您期望收到的不完全一致。