Home > front end >  I want to search through <dynamic> List in dart
I want to search through <dynamic> List in dart

Time:08-20

I have a list of products, the list is below:

List<dynamic>products = [
    {
        "id": 3,
        "name": "Apple MacBook Air 13.3-Inch 10th Gen Core i3-1.1GHz, 8GB RAM, 256GB SSD (MWTJ2) Space Gray 2020",
        "price": 126000,
        "descriptions": "Processor\tIntel Core i3-1000NG4 Processor (4M Cache, 1.10 GHz up to 3.20 GHz)\r\n\r\nMemory\t        8GB 3733MHz LPDDR4X onboard RAM\r\n\r\nStorage\t        256GB SSD\r\nGraphics\tIntel Iris Plus Graphics; **Support for Thunderbolt 3–enabled external graphics processors (eGPUs)\r\n\r\nDisplay\t        13.3-inch (diagonal) Retina LED-backlit display with IPS technology; 2560-by-1600 native resolution at 227 pixels per inch with support for millions of colors\r\n\r\nAdapter\t        30W USB-C Power Adapter; USB-C power port\r\n\r\nBattery\tUp to 10 hours battery back (Video & web browsing up to 05 hours depending on resolution) Built-in 49.9 watt-hours lithium-polymer battery\r\n\r\nOperating System\tmacOS\r\n\r\nNetworking\t                802.11ac Wi-Fi wireless networking; IEEE 802.11a/b/g/n compatible\r\n\r\nTouchpad\t                Multi-Touch gestures\r\n\r\nKeyboard\tBacklit Keyboard - US English Retina is equipped with: 78 keys (ANSI specification) or 79 keys (ISO specification), including 12 function keys and 4 direction keys arranged in an \"inverted T\" shape. Ambient light sensor Force touchpad brings precise cursors Control and pressure-sensing functions, support forceful long-press, acceleration function, pressure-sensitive drawing, and multi-touch gestures\r\n\r\nBluetooth\tBluetooth 5.0 wireless technology\r\n\r\nAudio\t        Stereo speakers; Wide stereo sound; Support for Dolby Atmos playback; Three-mic array with directional beamforming; 3.5 mm headphone jack\r\n\r\nOptical Drive  None\r\n\r\nSoftware\tFinal Cut Pro X; Logic Pro X\r\n\r\nWebCam\t720p FaceTime HD camera\r\n\r\nFingerprint\tYes\r\n\r\nDisplay Touch\tDisplay WithTouch ID sensor\r\n\r\nPorts & Connectors\t2 x Thunderbolt 3 (Supports DisplayPort and Power Delivery); Thunderbolt (up to 40Gb / s); USB 3.1 Gen 2 (up to 10Gb / s; 1 x HDMI; 1 x VGA; 1 x 3.5 mm Headphone Output\r\n\r\nColor\tSpace Gray\r\n\r\nDimensions (W x D x H)\tThickness: 0.41 to 1.61 cm (0.16 to 0.63 inches); Width: 30.41 cm (11.97 inches); Depth: 21.24 cm (8.36 inches)\r\n\r\nWeight\t1.29 kg (2.8 lbs)\r\n\r\nIncluded in the Box\tMacBook Air; 30W USB-C Power Adapter; USB-C Charge Cable (2 m)\r\n\r\nWarranty \t01 year International Limited Warranty (Terms & Condition Apply As Per Apple)",
        "image": "/images/macbook_IU3NvNF.jpg",
        "created_at": "2022-08-19T17:42:13.118095Z",
        "updated_at": "2022-08-19T17:42:13.118095Z",
        "stock": true,
        "category": 1,
        "user": "mosta"
    },
    {
        "id": 2,
        "name": "MSI Modern 14 B10MW Core i3 10th Gen 14\" Full HD Laptop",
        "price": 50890,
        "descriptions": "Processor   Intel Core i3 10110U Processor (4MB Cache, 2.1Ghz up to 4.1Ghz)\r\n\r\nDisplay        14.0\" FHD (1920x1080), IPS-Level Display\r\n\r\nMemory       4GB 2666MHz DDR4 RAM, (Suppoorted Up to 16GB)\r\n\r\nStorage        256GB NVMe SSD (Supported Up to 1TB)\r\n\r\nGraphics      Intel UHD Graphics\r\n\r\nOperating System     Free Dos\r\nBattery         39.3Whr\r\n\r\nAdapter       65W adapter\r\n\r\nInput Devices\r\nKeyboard    Backlight Keyboard\r\n\r\nWebCam     HD type (30fps@720p)\r\nCard Reader  1x Micro SD\r\n\r\nNetwork & Wireless Connectivity\r\nWi-Fi            802.11 ac Wi-Fi\r\n\r\nBluetooth   Bluetooth v5\r\n\r\nPorts, Connectors & Slots\r\nUSB (s)      2x Type-A USB2.0\r\n\r\n1x Type-C    USB3.2 Gen2\r\n\r\nHDMI        1x (4K @ 30Hz) HDMI\r\n\r\nAudio Jack Combo    1x Mic-in/Headphone-out Combo Jack\r\n\r\nExtra RAM Slot   NO\r\n\r\nExtra M.2 Slot     NO\r\n\r\nPhysical Specification\r\nDimensions (W x D x H)    319 x 220.2 x 16.9 mm\r\n\r\nWeight                1.3 kg\r\n\r\nColor(s)              Carbon Gray\r\n\r\nWarranty\r\nManufacturing Warranty    02 Year",
        "image": "/images/msi_sCwzOcI.jpeg",
        "created_at": "2022-08-19T17:41:53.358014Z",
        "updated_at": "2022-08-19T17:41:53.358014Z",
        "stock": true,
        "category": 1,
        "user": "mosta"
    },
    {
        "id": 1,
        "name": "iPhone 13 Pro Max",
        "price": 169000,
        "descriptions": "First Release\tSeptember 24, 2021\r\n\r\nColors\tGraphite, Gold, Silver, Sierra Blue\r\n\r\nNetwork\t2G, 3G, 4G, 5G\r\n\r\nSIM\tNano / Electronic SIM\r\n\r\nWLAN\t✅ dual-band, Wi-Fi hotspot\r\n\r\nBluetooth\t✅ v5.0, A2DP, LE\r\n\r\nGPS\t✅ A-GPS, GLONASS, GALILEO, BDS, QZSS\r\n\r\nRadio\t✖\r\n\r\nUSB\tLightning, USB 2.0\r\n\r\nOTG\t✅\r\n\r\nUSB Type-C\t✖ (Proprietary reversible connector)\r\n\r\nNFC\t✅\r\n\r\nStyle\tNotch\r\n\r\nMaterial\t        Gorilla Glass front & back, stainless steel frame\r\n\r\nWater Resistance\t✅ IP68 dust/water resistant (up to 6m for 30 mins)\r\n\r\nDimensions\t160.8 x 78.1 x 7.7 mmillimeters\r\n\r\nWeight\t240 grams\r\n\r\nSize\t6.7 inches\r\n\r\nResolution\t1284 x 2778 pixels (458 ppi)\r\n\r\nTechnology\tSuper Retina XDR OLED Touchscreen\r\n\r\nProtection\t✅ Scratch-resistant ceramic glass, oleophobic coating\r\n\r\nFeatures\tDolby Vision, HDR10, 1200 nits (max.), 120Hz, true-tone, wide color gamut\r\n\r\nResolution\tQuad 12 12 12 Megapixel   TOF 3D LiDAR scanner\r\n\r\nFeatures\tDual Pixel PDAF, sensor-shift OIS, dual-LED flash, ultrawide, depth, telephoto, 3x optical zoom & more\r\n\r\nVideo Recording\t4K (2160p), Dolby Vision HDR, ProRes, Cinematic mode, stereo sound rec.\r\n\r\nResolution\tDual 12 Megapixel   SL 3D\r\n\r\nFeatures\tF/2.2, HDR, 1/3.6″, depth / biometrics sensor\r\n\r\nVideo Recording\t4K (2160p), gyro-EIS\r\n\r\nType and Capacity\tLithium-ion 4352 mAh (non-removable)\r\n\r\nFast Charging\t✅ 27W Fast Charging (50% in 30 minutes)\r\n\r\nUSB Power Delivery 2.0\r\n\r\nWireless Charging\t✅ Fast Wireless Charging (15W MagSafe, 7.5W Qi magnetic)\r\n\r\nOperating System\tiOS 15\r\n\r\nChipset\tApple A15 Bionic (5 nm)\r\n\r\nRAM\t6 GB\r\n\r\nProcessor\tHexa-core, up to 3.22 GHz\r\n\r\nGPU\tApple GPU (5-core graphics)",
        "image": "/images/iPhone13promax_YiZ3ktK.jpg",
        "created_at": "2022-08-19T17:41:27.837767Z",
        "updated_at": "2022-08-19T17:41:27.837767Z",
        "stock": true,
        "category": 2,
        "user": "mosta"
    }
]

I want to search through the above list by product name. And also want a make separate list contain one single map.

If i search "apple" the result should be:

List<dynamic> searchResult = [
    {
        "id": 3,
        "name": "Apple MacBook Air 13.3-Inch 10th Gen Core i3-1.1GHz, 8GB RAM, 256GB SSD (MWTJ2) Space Gray 2020",
        "price": 126000,
        "descriptions": "Processor\tIntel Core i3-1000NG4 Processor (4M Cache, 1.10 GHz up to 3.20 GHz)\r\n\r\nMemory\t        8GB 3733MHz LPDDR4X onboard RAM\r\n\r\nStorage\t        256GB SSD\r\nGraphics\tIntel Iris Plus Graphics; **Support for Thunderbolt 3–enabled external graphics processors (eGPUs)\r\n\r\nDisplay\t        13.3-inch (diagonal) Retina LED-backlit display with IPS technology; 2560-by-1600 native resolution at 227 pixels per inch with support for millions of colors\r\n\r\nAdapter\t        30W USB-C Power Adapter; USB-C power port\r\n\r\nBattery\tUp to 10 hours battery back (Video & web browsing up to 05 hours depending on resolution) Built-in 49.9 watt-hours lithium-polymer battery\r\n\r\nOperating System\tmacOS\r\n\r\nNetworking\t                802.11ac Wi-Fi wireless networking; IEEE 802.11a/b/g/n compatible\r\n\r\nTouchpad\t                Multi-Touch gestures\r\n\r\nKeyboard\tBacklit Keyboard - US English Retina is equipped with: 78 keys (ANSI specification) or 79 keys (ISO specification), including 12 function keys and 4 direction keys arranged in an \"inverted T\" shape. Ambient light sensor Force touchpad brings precise cursors Control and pressure-sensing functions, support forceful long-press, acceleration function, pressure-sensitive drawing, and multi-touch gestures\r\n\r\nBluetooth\tBluetooth 5.0 wireless technology\r\n\r\nAudio\t        Stereo speakers; Wide stereo sound; Support for Dolby Atmos playback; Three-mic array with directional beamforming; 3.5 mm headphone jack\r\n\r\nOptical Drive  None\r\n\r\nSoftware\tFinal Cut Pro X; Logic Pro X\r\n\r\nWebCam\t720p FaceTime HD camera\r\n\r\nFingerprint\tYes\r\n\r\nDisplay Touch\tDisplay WithTouch ID sensor\r\n\r\nPorts & Connectors\t2 x Thunderbolt 3 (Supports DisplayPort and Power Delivery); Thunderbolt (up to 40Gb / s); USB 3.1 Gen 2 (up to 10Gb / s; 1 x HDMI; 1 x VGA; 1 x 3.5 mm Headphone Output\r\n\r\nColor\tSpace Gray\r\n\r\nDimensions (W x D x H)\tThickness: 0.41 to 1.61 cm (0.16 to 0.63 inches); Width: 30.41 cm (11.97 inches); Depth: 21.24 cm (8.36 inches)\r\n\r\nWeight\t1.29 kg (2.8 lbs)\r\n\r\nIncluded in the Box\tMacBook Air; 30W USB-C Power Adapter; USB-C Charge Cable (2 m)\r\n\r\nWarranty \t01 year International Limited Warranty (Terms & Condition Apply As Per Apple)",
        "image": "/images/macbook_IU3NvNF.jpg",
        "created_at": "2022-08-19T17:42:13.118095Z",
        "updated_at": "2022-08-19T17:42:13.118095Z",
        "stock": true,
        "category": 1,
        "user": "mosta"
    }]

How to approach to make this step?

CodePudding user response:

Here is one of approach you can use to search some query through the list and add the result in another list

simple search function

CodePudding user response:

import 'package:collection/collection.dart'; and use firstWhereOrNull; example:

searchArray(String searchValue){
     var test =
            products.firstWhereOrNull((element) => element['name'].contains(searchValue));
        print(test); //print just first object whos name property contains "MSI"

} Or

  searchArray(String searchValue) {
  
    var test =
        products.where((element) => element['name'].toString().contains(searchValue));
    if (test != null) {
      for (var testItem in test) {
        print(testItem); //print all maps whos contains sV
      }
    }
    
  }
  • Related