Certain midrashim are clearly not literal.
I was just learning in shiur last night Chagiga, daf 12b, where it says that a heaven that is a curtain is pulled over the sky every night and blocks the light from the sun and then is rolled back again every morning.
I don't assume that chazal took this literally. After all, they saw the sun set in the evening and rise on the opposite horizon every dawn. They knew that it wasn't a heavenly curtain that kept the sun's rays from reaching them at night, even if they might have thought the earth was a disc.
So the gemara is meant allegorically, and is meant to teach about Hashem's renewal of creation every morning, perhaps, or some other message.
But when chazal say that insects are spontaneously generated from a pile of dirt, it is hard to say they didn't mean it literally. It's not that they were advancing a false theory, they were just repeating the prevailing wisdom of the time, which we now know was false.