serialize method Null safety
- PlatformMenu item,
- PlatformMenuDelegate delegate,
- MenuItemSerializableIdGenerator getId
Converts the supplied object to the correct channel representation for the 'flutter/menu' channel.
This API is supplied so that implementers of PlatformMenu can share this implementation.
Implementation
static Map<String, Object?> serialize(
PlatformMenu item,
PlatformMenuDelegate delegate,
MenuItemSerializableIdGenerator getId,
) {
final List<Map<String, Object?>> result = <Map<String, Object?>>[];
for (final MenuItem childItem in item.menus) {
result.addAll(childItem.toChannelRepresentation(
delegate,
getId: getId,
));
}
// To avoid doing type checking for groups, just filter out when there are
// multiple sequential dividers, or when they are first or last, since
// groups may be interleaved with non-groups, and non-groups may also add
// dividers.
Map<String, Object?>? previousItem;
result.removeWhere((Map<String, Object?> item) {
if (previousItem == null && item[_kIsDividerKey] == true) {
// Strip any leading dividers.
return true;
}
if (previousItem != null && previousItem![_kIsDividerKey] == true && item[_kIsDividerKey] == true) {
// Strip any duplicate dividers.
return true;
}
previousItem = item;
return false;
});
if (result.isNotEmpty && result.last[_kIsDividerKey] == true) {
result.removeLast();
}
return <String, Object?>{
_kIdKey: getId(item),
_kLabelKey: item.label,
_kEnabledKey: item.menus.isNotEmpty,
_kChildrenKey: result,
};
}