So upset, my dog tried to hurt a kid

So I'm walking my dog the other day. she's always been so sweet to everyone, but it doesn't matter because a animal can snap at any time. So I'm walking my dog with my 5 year old and a little girl about 10 we know came running out. My dog has met her before and girl was just loving on her and patting her head. Then my dog dog snapped and jumped on her and ripped her shirt.. she did not bite her but if I wasn't holding on to her it's possible she might have. I'm so shocked because there was no reason for her to do this!. Little girl was fine but I'm probably more upset and mad it happened and I called her mom an apologize and they r fine. I'm so ashamed and embarrassed. But what would u do in this situation? Let it bother you? I told my husband he's like she probably wanted to play.. no she was scary mean. I don't want her to hurt anyone especially a child. I'll never take my dogs side if she hurt someone. But I told my husband she needs a muzzle and don't let kids or anyone pet her especially since she snapped out from no where.

Edit**** the dog was on a very short leash** never said she was off the leash. Not a small dog either. I was holding her at the collar too.

Edit** I appreciate the input. As a pet owner u need to take responsibility for your pets. No excuses! So I learned now my dog doesn't want people petting her head or getting too close. But I'm not taking chances so I'm going to go out and buy a muzzle for walks. Thanks