Your general approach seems okay. It’d take some debugging to work out where the disconnect is.
I created a very simple test app to try it out, and it seems to be working fine, so maybe take a look at it and see where your approach differs: https://anvil.works/build#clone:5ZHGEJTYJ2ZO26BA=XWGSZZ72ONLS66QR67ICLEG2