  An unidentified $\gamma$-ray source 4FGL J1838.2+3223 has been proposed as a
pulsar candidate. We present optical time-series multi-band photometry of its
likely optical companion obtained with the 2.1-m telescope of Observatorio
Astron\'omico Nacional San Pedro M\'artir, Mexico. The observations and the
data from the Zwicky Transient Facility revealed the source brightness
variability with a period of $\approx$4.02 h likely associated with the orbital
motion of the binary system. The folded light curves have a single sine-like
peak per period with an amplitude of about three magnitude accompanied by fast
sporadic flares up to one magnitude level. We reproduce them modelling the
companion heating by the pulsar. As a result, the companion side facing the
pulsar is strongly heated up to 11300$\pm$400 K, while the temperature of its
back side is only 2300$\pm$700 K. It has a mass of 0.10$\pm$0.05 ${\rm
M}_\odot$ and underfills its Roche lobe with a filling factor of
$0.60^{+0.10}_{-0.06}$. This implies that 4FGL J1838.2+3223 likely belongs to
the `spider' pulsar family. The estimated distance of $\approx$3.1 kpc is
compatible with Gaia results. We detect a flare from the source in X-rays and
ultraviolet using Swift archival data and another one in X-rays with the
eROSITA all-sky survey. Both flares have X-ray luminosity of $\sim$10$^{34}$
erg s$^{-1}$ which is two orders of magnitude higher than the upper limit in
quiescence obtained from eROSITA assuming spectral shape typical for spider
pulsars. If the spider interpretation is correct, these flares are among the
strongest flares observed from non-accreting spider pulsars.
