It takes around two weeks for fly eggs to hatch into flies. Sawdust bucket type toilets that are emptied more frequently than every two weeks won’t have flies to worry about. Larger containers that fill for longer than two weeks will probably have an occasional fly outbreak. First, try to prevent flies from getting in. Make sure the toilet lid fits snugly onto the soil seat. Install screens on any vent pipes and caulk any cracks where flies could enter.

During an infestation, if possible, empty the toilet and clean out the container. Or, purchase neem oil from a nursery or garden store. Dilute it in a spray bottle and spray into the toilet for a few days. Neem oil is effective on both fly larvae and eggs.
Learn how to build a simple fly trap from compost toilet pioneer David Omick (scroll down to find the instructions here)