Embedded view not refreshed (only for Android) #512
-
|
With Skip Lite (installed last week), with the following code (example), the city name in the view included is refreshed on iOS but not refreshed on Android. Why? What is the best practices? import SwiftUI
struct TestView: View {
/// View properties
@State private var refreshPhrase = false
let cities: [String] = ["Paris","Bordeaux","Lyon","Cannes","Calais","Ajaccio"]
var body: some View {
@State var randomCity = cities.randomElement()!
VStack {
Text("TestView")
VStack {
Text(randomCity) // refreshed on both iOS and Android
.padding()
.background(.yellow)
.padding()
CityView(city: randomCity) // the city is refreshed on iOS and is not refreshed on Android
.border(.black)
.padding()
Button {
refreshPhrase = !refreshPhrase // turnaround for .toggle() unavailability
randomCity = cities.randomElement()!
} label: {
Text("Refresh")
}
.padding()
Spacer()
}
.border(.black)
.id(refreshPhrase)
}
}
}
struct CityView: View {
@State var city:String
var body: some View {
VStack {
Text("in City View:")
.padding()
Text(city)
.padding()
.background(.green)
.padding()
}
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
|
Do not use @State private var. just use @State var as a @State private var won’t bridge to AndroidMay God bless you in a special way! On Oct 11, 2025, at 12:29 PM, lhoumeau ***@***.***> wrote:
With Skip Lite (installed last week), with the following code (example), the city name in the view included is refreshed on iOS but not refreshed on Android. Why? What is the best practices?
import SwiftUI
struct TestView: View {
/// View properties
@State private var refreshPhrase = false
let cities: [String] = ["Paris","Bordeaux","Lyon","Cannes","Calais","Ajaccio"]
var body: some View {
@State var randomCity = cities.randomElement()!
VStack {
Text("TestView")
VStack {
Text(randomCity) // refreshed on both iOS and Android
.padding()
.background(.yellow)
.padding()
CityView(city: randomCity) // the city is refreshed on iOS and is not refreshed on Android
.border(.black)
.padding()
Button {
refreshPhrase = !refreshPhrase // turnaround for .toggle() unavailability
randomCity = cities.randomElement()!
} label: {
Text("Refresh")
}
.padding()
Spacer()
}
.border(.black)
.id(refreshPhrase)
}
}
}
struct CityView: View {
@State var city:String
var body: some View {
VStack {
Text("in City View:")
.padding()
Text(city)
.padding()
.background(.green)
.padding()
}
}
}
—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you are subscribed to this thread.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
|
Thank you Dave. Nevertheless the @State var (without private) has no effect. |
Beta Was this translation helpful? Give feedback.
-
|
You’re recreating a state inside CityView. On Android, it may not update like it does on iOS. Try removing @State or changing it to @binding. I believe that will make it work. |
Beta Was this translation helpful? Give feedback.
-
|
Thank you Vitor. The good way (with a counter added when the random draw of the city give the previous one) : import SwiftUI
struct TestView: View {
/// View properties
let cities: [String] = ["Paris","Bordeaux","Lyon","Cannes","Calais","Ajaccio"]
@State var refreshPhrase:Bool = false
@State var count:Int = 1
var body: some View {
@State var randomCity = cities.randomElement()!
VStack {
Text("TestView")
VStack {
Text("\(randomCity) (\(count))") // refreshed on both iOS and Android
.padding()
.background(.yellow)
.padding()
CityView(city: randomCity, count: count) // the city is refreshed on iOS and is not refreshed on Android
.border(.black)
.padding()
Button {
refreshPhrase = !refreshPhrase // turnaround for .toggle() unavailability
count += 1
randomCity = cities.randomElement()!
} label: {
Text("Refresh")
}
.padding()
Spacer()
}
.border(.black)
.id(refreshPhrase)
}
}
}
struct CityView: View {
var city:String
var count:Int
var body: some View {
VStack {
Text("in City View:")
.padding()
Text("\(city) (\(count))")
.padding()
.background(.green)
.padding()
}
}
} |
Beta Was this translation helpful? Give feedback.
You’re recreating a state inside CityView. On Android, it may not update like it does on iOS. Try removing @State or changing it to @binding. I believe that will make it work.