Mismatch between widget size and @Environment value in TimelineProvider

Morning, everyone,

I'm developing a widget using an API that sends an array of different sizes depending on the size of the widget (small : 3 news, medium: 6 news, large: 9 news).

I have the impression that there is a problem between the @Environment(\.widgetFamily) var and the timeline function (where I call the API) of TimelineProvider.

Indeed in this function the environment variable is always equal to the systemMedium size despite the "real" size of the widget.

Do you also have the same problem on your side ?
Is it a known bug from Apple ?
How can I solve this bug ?

Thanks for your help :] 

Answered by tabolstad in 626126022
I am seeing the same behavior in Beta 4, the environment always reports the widgetFamily as .systemMedium
No API involved in my case, just static data sent from the timeline.
Accepted Answer
I am seeing the same behavior in Beta 4, the environment always reports the widgetFamily as .systemMedium
No API involved in my case, just static data sent from the timeline.
The way that I am dealing with this is to generate the same array for all widgets and then limit it on the view side where the @Environment(\.widgetFamily) does work. In your case, you can have your timeline always generate 9 items in the array, then in your view you can do something like the following:

Code Block    
func getArray(array: [String]) -> [String] {
if (family == .systemSmall) {
return Array(array.prefix(3))
} else if (family == .systemMedium) {
return Array(array.prefix(6))
} else {
return array
}
}


The above solution by jcgoforth doesn't seem to work for me. Even inside the view, the \.widgetFamily is returning .systemMedium when the view is large.
Hi, I'm using the macOS beta 1 (yes... beta1 :( sadly on public beta). But I can render my widgets on multiple sizes using the @Environment(\.widgetFamily).

You can consult my code on ToDo-SwiftUI on Github here.
Has this issue been fixed? I am still seeing it in iOS 14.0.1.
Mismatch between widget size and @Environment value in TimelineProvider
 
 
Q