简体   繁体   中英

How to filter strings from string type of list in Flutter?

在此处输入图像描述

List availableSlots = ["09:00 AM","09:15 AM","09:30 AM","09:45 AM","10:00 AM","10:15 AM","10:30 AM","10:45 AM","11:00 AM","11:15 AM","11:30 AM","11:45 AM","12:00 PM"];

So, availableSlots is string type of list. Now, you can see list of slots in image B. User will get same list on click of "start time" & "end time" Button that is in image A. So, I want to set validation there, if user select "10:00 AM" as a start time of restaurant. Then, when the user click on "end time" button, there should be filtered list available slots are ["10:15 AM","10:30 AM","10:45 AM","11:00 AM","11:15 AM","11:30 AM","11:45 AM","12:00 PM"] only. Bacause, User get selected 10:00 AM as start time. so, it is but obvious that, end time should not be past time. 在此处输入图像描述

You can do something like this:

var start = "10:15 AM";
var availableSlots = ["09:00 AM","09:15 AM","09:30 AM","09:45 AM","10:00 AM","10:15 AM","10:30 AM","10:45 AM","11:00 AM","11:15 AM","11:30 AM","11:45 AM","12:00 PM"];
var availableEndSlots = availableSlots.sublist(availableSlots.indexOf(start) + 1);
print(availableEndSlots);

Output:

[10:30 AM, 10:45 AM, 11:00 AM, 11:15 AM, 11:30 AM, 11:45 AM, 12:00 PM]

Don't pass availableSlots directly to the endTime picker, use filtered list for that like this:

itmes: availableSlots.getRange(availableSlots.indexOf(selectedStartTime)+1, availableSlots.length).toList()

and make sure that this code runs every time you change the selectedStartTime.

You can use this approach

List availableSlots = [
  "09:00 AM",
  "09:15 AM",
  "09:30 AM",
  "09:45 AM",
  "10:00 AM",
  "10:15 AM",
  "10:30 AM",
  "10:45 AM",
  "11:00 AM",
  "11:15 AM",
  "11:30 AM",
  "11:45 AM",
  "12:00 PM"
];

List endTimeSlotes = List.from(availableSlots);

endTimeSlotes.retainWhere(
  (element) {
    DateTime dateTime = format.parse("10:00 AM");
    int compareResult = DateFormat.jm().parse(element).compareTo(dateTime);
    return compareResult == 1;
  },
);

FYI:- https://gist.github.com/Nitingadhiya/0e7e4e252f7fcf7526af1fc63a430637

Get Difference between 2 time.

 List availableSlots = ["09:00 AM","09:15 AM","09:30 AM","09:45 AM","10:00 AM","10:15 AM","10:30 AM","10:45 AM","11:00 AM","11:15 AM","11:30 AM","11:45 AM","12:00 PM"];
  
  String timer = "09:30 AM";
  String timer1 = "05:40 PM"; 
  
  DateTime dateOne = DateFormat.jm().parse(timer);
  DateTime dateTwo = DateFormat.jm().parse(timer1);
 
  final Duration duration = dateTwo.difference(dateOne);
  print("${duration.inHours} hours");
  print("${duration.inMinutes} minutes");

//Example
     DateTime date= DateFormat.jm().parse("6:45 PM");
     DateTime date2= DateFormat("hh:mma").parse("6:45PM"); 
     // format date
     print(DateFormat("HH:mm").format(date));
     print(DateFormat("HH:mm").format(date2));

This will work even without sorting the list

import 'package:intl/intl.dart';

void main() {
  var start = "10:00 AM";
  var availableSlots = [
    "11:30 AM",
    "09:00 AM",
    "10:45 AM",
    "09:15 AM",
    "11:45 AM",
    "09:30 AM",
    "09:45 AM",
    "10:00 AM",
    "10:15 AM",
    "10:30 AM",
    "11:00 AM",
    "11:15 AM",
    "12:00 PM"
  ];
  final startDate = DateFormat().add_jm().parse(start);
  var availableEndSlots = availableSlots.map((slot) {
    final formattedTime = DateFormat().add_jm().parse(slot);
    if (startDate.isBefore(formattedTime)) {
      return slot;
    }
  }).where((element) => element != null).toList();
  print(availableEndSlots);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM