在给NavigationLink添加destination报错Result values in '? :' expression have mismatching types 'ContentView' and 'BadgeBackground'
报错代码
NavigationLink(
destination: model.id == 0 ? ContentView() : BadgeBackground()
) {
Text(model.name)
}
解决方式:
使用AnyView对ContentView和BadgeBackground进行转换
修正后代码:
NavigationLink(
destination: model.id == 0 ? AnyView(ContentView()) : AnyView(BadgeBackground())
) {
Text(model.name)
}
参考链接: https://stackoverflow.com/questions/59060321/navigationlink-with-conditional-in-a-list