SD card works in setup but not in loop

Try to write a minimum compilable sketch for your devices that can repeat the errors.