如何将鸽子中的类型指定为Map (例如Map<String, String>
),最好是具有动态值类型的映射(Map<String, dynamic>
)。在发送推送消息之前,我无法确定data
值是什么类型。
尝试1
我尝试使用以下方法来定义一个类:
class RemoteMessage {
Notification? notification;
Map<String, dynamic>? data;
}
不幸的是,我收到了一条错误消息:
Error: pigeons/push.dart:6: Generic type arguments must be nullable in field "data" in class "RemoteMessage".
Error: pigeons/push.dart:6: Generic type arguments must be nullable in field "data" in class "RemoteMessage".
企图2
我还尝试将dynamic
设置为可选的:
class RemoteMessage {
Notification? notification;
Map<String, dynamic?>? data;
}
在这种情况下,我只得到错误的一个实例:
Error: pigeons/push.dart:6: Generic type arguments must be nullable in field "data" in class "RemoteMessage".
尝试3
如果我将键类型设为可选(即Map<String?, dynamic>? data;
),则会得到错误:
Unhandled exception:
FileSystemException: Cannot open file, path = './android/app/src/main/java/dev/flutter/pigeon/Pigeon.java' (OS Error: No such file or directory, errno = 2)
#0 _File.open.<anonymous closure> (dart:io/file_impl.dart:356:9)
<asynchronous suspension>
pub finished with exit code 255
摘要
看起来鸽子不支持Map
或dynamic
,尽管它应该已经支持泛型:https://github.com/flutter/flutter/issues/63468。
发布于 2021-12-08 21:28:06
在一些关于颤振(1和2)的问题上,斯图尔特·摩根(一位来自谷歌的开发人员可能在Dart /鸽子上工作)给了我一些帮助,我意识到我的课程应该是这样的:
class RemoteMessage {
Notification? notification;
Map<String?, Object?>? data;
}
主要外卖:
Object
应该使用而不是 dynamic
,,因为鸽子会在Java、Objective和Dart中生成中断的代码。这是我在使用动态生成代码..。:中报告的一个bugdynamic.decode()
@property(nonatomic, strong, nullable) NSDictionary<NSString *, dynamic *> * data;
public void setData(Map<String, dynamic> setterArg) { this.data = setterArg; }
?
,包括使用String?
作为映射键的类型,而不是String
:Map<String?, Object?>
。https://stackoverflow.com/questions/70233385
复制相似问题