The “proper” way to do this is with split-horizon DNS. What this means is you have the FQDN of your server registered in public DNS with the public IP, and you run a local DNS server on your LAN that gives out the LAN IP for that same FQDN.
The end result of this is that your clients always use the same name to access it, and it gets the correct IP from DNS whether it is local or remote, and it “just works.”
To be fair, this is not really a Nextcloud question since this applies to pretty much anything self-hosted. I run pfSense for my firewall and use its built-in DNS resolver to do this, but any DNS server will work.