I have a diary up on dealing with them.
It has many methods, depending what you have on hand.
Also I've learnt other stuff to get rid.
But basically. You need to implement to methods.
1 for the fliers
1 for the eggs that have been laid.
You need to do these things both in conjunction with each other.
So, yellow sticky traps, fruit fly traps.
Hang them near your lights on string at dif levels.
Then, you need to sort the larva out. So you'll need to let the soil dry out. And any larva that have turned into gnats in your pot that are waiting on their wings.
So. Bury traps and push soil up around the edges so they've to crawl over it. And if you put some cinnamon in thy middle its a natural fungi that they'll go to and hence get trapped.
You can feed water from the bottom up for a few days. (This might cause issues of its own depending how long you have to do it that way)
And soil dried out. Scrape a few CM off the top. So any inert larva won't hatch once you wet soil again.
Have a look at my diary.
I go into how you can use ethanol/water mixes to kill larva or you can use H202/water but i take it u on the organic route..hence the flies.
Mainly they come in your bags of soil. And then when you water the larva already in the soil goes active.
If you need any help more on this feel free to DM me
In future. Treat your soil by way of putting in oven at 220c for 30mins. Will leave a smell for a bit though.
So I prefer treating my all mix/light mix soil by boiling water pouring through the pot.
If you have a hot enough shower you can just use that. It'll kill any larva. You'll have soaked soil.. so when potting, place some treated soil that you've separated and dried out.. then placing your seedling in the dry soil it'll give it time to grow and by the time it gets to the wet, that should be at a stage where it'll be a lot drier. (Or you can just let the soil dry out. But that'll take days.
Feel free to DM me. There's a lot more on my diary, gnats, fruit flies.
Good luck.