Cant display change in information of oled display

Hello. I have a problem with my code. I can't understand where i'm making mistake, but i'm sure i have. I'm new to programing and don't kwot most of the things. Now i want to display information on oled display with IR sensor (to show when there is detected an object, and when there is not). When i upload the code the display shows either detected or not detected(depending if the data in the bracets is LOW or HIGH).i try to change small parts of the code that i think may cause this issue but nothing changes.
here is the code:

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 
#define SCREEN_HEIGHT 64
#define OLED_RESET  -1 
#define SCREEN_ADDRESS 0x3C 
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

int sensor = 18;

void setup() {
   if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS, OLED_RESET)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); 
  }
  // put your setup code here, to run once:
  Serial.begin(115200);
 
}

void loop()
{
   int data = digitalRead(sensor);
   display.clearDisplay();
   display.setTextColor(WHITE);
   display.setTextSize(2);
   display.setCursor(0,0);
   if (data == LOW)  display.println("Detected");
  else display.println("Not Detected");
   display.display();
   delay(1000);
}   

I'm open to any tips
I think that the problem may be in the IF and ELSE function or in the int, but i may be wrong
The purpose of this project is when i place my hand or object to display "detected", and when removed to display "not detected"

Which type of arduino are you using? Pin 18 on an UNO/Nano is A4, which is also SDA of the I2C bus.

The code is working correctly. Please post a link the IR sensor, and a hand drawn diagram showing how you wired it, with pins and connections clearly labeled, including power and ground.

Most likely, the sensor outputs a brief signal that the code either misses, or the display is overwritten so rapidly that you don't see the expected response.

Sampling the sensor every second is a sure way to miss a brief pulse. So, assuming that the sensor briefly outputs a LOW for signal detected, one approach is to wait for that.

while(digitalRead(sensor) == HIGH);    //wait here for a LOW reading.
// display signal here

Forgot to mention its esp32

Try display.print()

  if (data == LOW)  display.print("Detected");
  else display.print("Not Detected");

Just a thought.