i had the same problem in my gecko cages...
use a little bit of dish soap mixed in with water and put it directly into the plants dirt.
usually takes a day or two to take effect. the current ones will remain until they die, but the eggs and larvae in the dirt will not hatch and or die.
you might have to do it a few times but it works. and it does not harm the plants.
i been gnat free for 3.5 months
sorry i havent bee on in a bit..
it should be ok. i ussually wash off any excess soap on any rocks or dirt just in case..
i use a spray bottle so it foams up a bit. then i rinse the bottle well and put fresh water and respray so the soap isnt on the surface..
if you have rocks it should not be a big deal. just rinse the top of the rocks with fresh water ant the gnats should go away..
i did it in my gecko terraniums with them in it and they do ok..