Are there any ways to detect if the opponent is setting a trap, without explicitly just "knowing" the position?
The most important thing to have is a sense of tactical danger, and this comes about as a combination of tactical intuition, and calculation ability. Intuition comes from playing lots of games, winning about half and losing about half --- from the losses, many of those will be from tactical blunders, which will train your brain emotionally to be averse to such blunders, and eventually it enters your subconscious and becomes intuition. Calculation ability can be trained by similarly playing lots of games, but also specific tactical training e.g. solving puzzles, analysing complicated middlegame positions or solving studies, etc.
Concretely, it is helpful to be aware of the idea of "compensation". If your opponent is giving away pawns or pieces in the opening, you should always be on high alert and ask yourself: did they just blunder that, or do they have a plan? If they have a follow-up plan, is it good? This follow-up might be in the form of a 1-move tactic that wins back the material, or it might be some kind of "long term compensation", meaning e.g. a strong mating attack.
[title ""] [fen ""] 1.e4 e5 2.Nf3 Nc6 3. Nc3 Nf6 4. Bc4 Nxe4
This is a mild example of a "trap" that happens very often in beginner games, where 4.Bc4 is already an inaccuracy because of 4...Nxe4 seemingly giving up a piece. The "trap" is that after 5. Nxe4, black has 5...d5 to win back the piece, and black is already equal and white is not better. So, if you have a good sense of tactical danger, you will know to avoid this "center fork trick" and not play 4.Bc4. (This line is playable, and apparently has even been played by Wesley So in a rapid game, but it is really not good for White.)
But even more serious are the traps that lead to a losing position for one side.
[title ""] [fen ""] 1. d4 e5 2. dxe5 Nc6 3. Nf3 Qe7 4. Bf4 Qb4+ 5. Bd2 Qxb2 6. Bc3 Bb4 7. Qd2 Bxc3 8. Qxc3 Qc1#
For example there is this famous trap in the Englund gambit. How to avoid falling for such things? Well, you should first notice as White the existence of the forcing move Qb4+ forking the bishop and the pawn on b2. (A lot of people at beginner level would play a move like 5.Nbd2 hanging the bishop.) Then, you should notice that 6.Nc3 is the only move, because this line with 6...Bb4 is losing, given the pin to your king.
There is obviously no one size fits all solution to never ever fall into an opening trap again. Famously, GM Elshan Moradiabadi lost in 10 moves against GM Lenier Dominguez Perez in the US Championships 2022!
[title "Dominguez vs Moradiabadi, US Champs 2022"] [fen ""] 1. e4 e5 2. Nf3 Nf6 3. Nxe5 d6 4. Nf3 Nxe4 5. d4 d5 6. Bd3 Be7 7. O-O Bf5 8. c4 O-O 9. cxd5 Qxd5 10. Bxe4 1-0
The big mistake was 8...O-O and the game was well and truly lost after 9...Qxd5. Black resigned because Re1 is coming to pick up the hanging bishop on e7, either on the next move if Qxe4, or two moves later after Nc3, queen moves, and Nxe4.
The only advice that can be given is to strengthen your tactical sense of danger, so that you are more aware to traps like these. Another point is to simply keep playing chess, and after thousands of painful losses, you will eventually learn so many traps that you are unlikely to fall into those again. Once you're strong enough, maybe 2000+, it could be worth the energy to invest time into studying openings specifically as well, which will further improve your opening play so you don't lose to such traps again. However, as we have seen from the previous example, you will never stop making blunders, you can only seek to decrease their frequency. :)
You might not have alluded to Possibility B, but I add it for completeness: If you know the opponent good enough, you can scan for "Poker tells".
I give no warranties though, as this little story proves:
I played a dude in the chess club. Selfsame dude was...well, a certified jerk. Pet theory: The more jerkish you are, the better you can "read" the opponent (evolution at work). I was the stronger player, but I stood abysmal and tried a last trap: Played my move and ohbotherthatwasalousyoneimsuchapatzer-ed. He looked into the position - man, what a lame trap. Of course he immediately looked through my body language and played another move.
Which was actually far worse than accepting the "trap", which would have royally backfired if calculating a bit deeper. Or in one word: