What I have noticed with gnats, anything that has moisture, like wet paper towels in the trash bin that doesn't dry up, and with used kitty litter just sitting there gets gnats. So, when I use paper towels I let them dry before throwing them away. When I have food that I don't eat I'll use a plastic bag to put the food in and take it to the trash bin. Never do I put it the trash bin inside especially during the summer. During the winter I don't have any gnats, but when it gets warmer it seems this is when gnats start showing up.
I do use small kitchen counter size trash bags to tightly bag up scraps, peels, crumbs and such, because I don't want to get ants.
I guess my moisture offense is to have dishes soaking in the sink, like pans I cooked in, so they'll come clean in the dishwasher.
I just don't understand why most years there are no gnats, at least not indoors, or at least not in my apartment, and this year they are legion. Like I once read that when deer and elk have a bonanza year, with more live births, healthy babies, the wolves that prey on them will also have a bonanza year in terms of population growth, a strange but documented form of unexplained synergy. So what is the synergy that is working for the gnats? We haven't had particularly a lot of rain, no more than usual, which is never a lot in south Texas, and my landlady doesn't use sprinklers.