Sensitive and selective detection of trace aflatoxin B1 (AFB1) in foods is of great importance to guarantee food safety and quality but still challenging because of its trace amount and the interference from the complex food matrix. Here, we report the integration of aptamer (Apt) and an ordered 2D covalent organic framework (COF) to solid-state anodic aluminum oxide (AAO) nanochannels (Apt/COF/AAO) for selective and sensitive detection of trace AFB1. The high specificity of Apt for AFB1 led to a selective change in the surface charge of Apt/COF/AAO and in turn the current change of the nanochannel, permitting the selective and sensitive determination of trace AFB1 in complex food samples. The developed nanofluidic sensor gave a wide linear range (1-500 pg mL-1), low detection limit (0.11 pg mL-1), and good precision (relative standard deviation of 1.5% for 11 replicate determinations of 100 pg mL-1). In addition, the developed sensor was successfully used for the detection of AFB1 in food samples with the recovery of 86.9%-102.5%. The coupling of Apt-conjugated 2D COF with an AAO nanochannel provides a promising way for sensitive and selective determination of food contaminants in complex samples.