Try adding more brown, carbon-rich materials to your compost and make sure it is not too moist. If this does not work, you can add lime or citrus foods and cover any small entrances however it may be best to let them be as they help break down nitrogen rich food.
Add More Brown Matter
The most common people have maggots in there compost is because their green to brown ratio is off. Make sure you are adding around 2 parts of brown materials for every part of green to ensure your compost is well balanced and does not attract the flies in the first place. Also ensure that your green matter is buried at least 6 inches below the brown matter which will also help steer away the flies.
It Might be Too Moist
Another very common reason might be that your compost is too moist which are the perfect conditions for maggots to thrive in. While we have an entire article dedicated to treating this very problem, the best solutions are to add more brown matter to absorb the moisture and ensure water is draining out from the bottom.
Add some Citrus
If both of the above solutions do not work and your problem is really getting out of hand, remember having a few maggots here and there is fine, then you may want to consider increasing the pH of the pile. You can do this by adding around a cup of lime for every 25 cu feet. of compost or simply add some more citrus foods. However, be careful with this because adding too much could make the pH of your compost too high.
Don't Let Flies In
Because maggots were once eggs which flies laid in your compost, the most obvious solution may just be to not let the flied in by blocking any entrances. While you should not completely close of your compost to the environment as it does need proper airflow to decompose, don’t leave large openings and you could cover smaller ones with mesh.