H.A.F.
a.k.a. Rusty Nails
If you are referring to storebought castings then maybe. I have two stackable-tray worm bins in my spare bedroom with a big pot of resting soil and a few already harvested pots sitting by the window just letting the cover crops die off while the stuff in the soil processes the root ball. I have no gnats whatsoever. And that's with a garden out the back door.It seems like, if even say the word..worm castings..fungus gnats start showing up.
The fresh castings from live worms don't just have worm-poop. It is alive with smaller microorganisms like your nematodes and such, but there are beneficial mites, and sometimes springtails and rove beetles. All of these things stay in their happy little home rather than venturing out into your room because the pot is nice and warm and moist like they want it. And the population of these beneficial things create a positive 'pest-pressure' that doesn't allow invasive species to take hold. If a gnat lays some eggs they eat that shit.
There are also natural pest management things you can use as a preventative. I was as skeptical as they come when I started. I didn't want to let go of the calmag, and I did have gnats at first, from the fact that my soil wasn't alive yet. Fresh worm castings were actually what I used to finally beat them down and eradicate them forever.