Ant-Man 2 also released between Infinity War and Endgame and didn't do nearly as well as Captain Marvel.
To put things into better perspective:
Avengers Infinity War release year: April 2018
Ant-Man 2 release year: July 2018
Captain Marvel release year: March 2019
This is what I feel helped CM and what hurt Ant-Man 2:
1) As a bonus boost, Captain Marvel was teased at the end of IW and Samuel Jackson Nick Fury was present in the trailers as the film's co-star to promote it.
2) She was marketed as a powerful key figure that would turn the tide against Thanos, who just won and killed half of everything, which put a large amount of importance on her for the people who just saw IW to watch this movie. In fear of missing out(FOMO), they decided to go watch this movie. Edit: I'm not saying
everyone did this, but I can bet that a lot of people did this.
3) Ant-man's story was still disconnected from the Avengers storyline, with this even being present in the trailers, accurately presenting the movie as half-comedy half-action in typical marvel fashion. It disregarded the very serious tone their main arc just went into.
My claim is that most of those billion+ watchers of Infinity War saga wanted to mainly tune in to the Infinity War saga to see it to it's conclusion. Captain Marvel was given importance to this saga as a Superman-like figure. Comparatively, Ant-Man 2 was a poorly timed release and sent out as essentially a fun filler-arc movie to explain away 'where was Ant-Man during these events'.