PITTSBURGH — Eventually a starting pitcher was going to throw a clunker, forcing the Mets to find another way to win a game.

That test arrived Friday night, and the Mets flopped. Steven Matz was knocked out in the fourth inning — snapping the string of excellent starting-pitching performances — and the Mets’ lineup and bullpen went flat in an 8-4 loss to the Pirates at PNC Park that ended their seven-game winning streak.

The Mets (53-56) had enjoyed success as their starting pitchers went 7-1 with a 1.47 ERA over the previous 12 games, and Matz started on that course over three innings before getting flattened in the fourth, when the Pirates scored five runs.

“We weren’t going to go undefeated the rest of the way,” manager Mickey Callaway said. “Let’s just start another streak.”

Tyler Bashlor flushed the Mets’ chances of a comeback victory by surrendering a three-run homer to Starling Marte in the seventh that gave the Pirates an 8-4 lead. Bashlor allowed two singles in the inning before Marte cleared the left-field fence for his 19th homer of the season.

Shortstop Kevin Newman’s error in the seventh on Robinson Cano’s pop-up to shallow center allowed Michael Conforto to score, bringing the Mets within 5-4. But Newman’s backhanded stop on Wilson Ramos’ grounder and throw-out prevented the tying run from scoring.

Callaway said he would have used Seth Lugo instead of Bashlor if the game had been tied in the seventh, but he wasn’t about to deploy his best setup option with the Mets behind. Already, Robert Gsellman had been used for 2¹/₃ scoreless innings following Matz.

“We were a groundball that Ramos hit, maybe six more inches to the left, of Lugo being in the game in a tie game,” Callaway said. “That’s how close the game is. It’s a game of inches and sometimes it changes who is going to be out there pitching or hitting.”

Matz extended his scoreless innings streak to 14, but then failed to survive the fourth, as the Pirates batted around and took a 5-3 lead.

Melky Cabrera delivered the biggest hit in the inning, a two-run double that bounced off the top of the center-field fence and put the Pirates ahead for the first time. Elias Diaz’s two-out RBI single brought home the inning’s final run.

Overall, Matz lasted 3 ²/₃ innings and allowed five earned runs on six hits with one walk and four strikeouts. In his previous appearance — against the Pirates last Saturday at Citi Field — the left-hander pitched his first career shutout.

Matz walked Bryan Reynolds to begin the fourth inning Friday before allowing singles to Marte, Josh Bell and Jose Osuna that sliced the Mets’ lead to 3-2. Cabrera’s go-ahead double followed.

“In that situation there, it just snowballed so fast,” Matz said. “I was out of the game and was like, ‘I can’t believe I’m out of this game, especially with how I went through the first time through the lineup.’

“I was just looking back at video to see what went wrong and I think I should have taken a breather for a second and just tried to execute a pitch. Instead I kept going and going and the balls were up in the zone and you give guys a chance that way.”

The Mets had jumped on Pirates starter Trevor Williams for two runs in the first inning. After Jeff McNeil doubled on the game’s first pitch, Pete Alonso smashed an RBI double and Ramos’ groundout later in the inning drove in a second run.

J.D. Davis delivered a run-scoring double in the third after Alonso had walked to begin the inning and Cano singled for his second hit of the game.

Williams (4-4) pitched six innings for the Pirates and allowed three earned runs on six hits.