I am having a heck of a problem trying to figure out how to add Sections to a List in SwiftUI (dynamically based on a change in an array value). Everything works until I try and test a variable and add a section around the row. As soon as I add some logic I get the error: Unable to infer complex closure return type; add explicit type to disambiguate. I tried moving the section to a new function returning a View but that didn't help either. Any thoughts on how to rewrite this: The SiteRow() is defined as a struct of some View and returns some stacks. I couldnt find any examples on how to do a normal list row and add a section dynamically, they were all hardcoded lists.struct FetchView: View { @ObservedObject var fetcher = NOCFetcher() var lastCompany: String = "" var body: some View { NavigationView { VStack { List(fetcher.siteinfo) { // The Unable to infer complex closure error is shown here at the curly bracket site in if site.customer != self.lastCompany { Section(header: Text(site.customer)) { SiteRow(site: site) } self.lastCompany = site.customer } else { SiteRow(site: site) } }.navigationBarTitle(Text("Site Listing")) } } }}
Post
Replies
Boosts
Views
Activity
I am really tring to understand why I can't put any code that just changes a var inside of the var body = some View section. I can test vars and do stuff but I need to change a var / @State inside and as soon as I add that line I get error "Unable to infer complex closure return type; add explicit type to disambiguate". I have really simplified this down (bogus code for example, real code is looping through an array list. When the one line "lastSection = '1234'" is added, that causes the code. I think this is because a view is only expecting a "View" Type like Text, Image, List, etc and doesn't know what to do with the code. How else are you supposed to actually set / change anything dynamically in a View section?struct FetchView: View { @State var lastSection = "" var body: some View { NavigationView { VStack { Text("1234") if lastSection != "1234" { Text("A") lastSection = "1234" } }.navigationBarTitle(Text("Site Listing")) } } }}