flutter 如何从列表访问属性并填充小部件?

6ojccjat  于 2023-01-31  发布在  Flutter
关注(0)|答案(1)|浏览(100)

我有一个对象列表:

List<Doctor> doctorsList = [
  Doctor(
      name: "Doctor 1 Olly",
      type: "Dentist",
      info:
          "At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga.",
      star: "4.9",
      imageUrl: "img/doctors/doc1.jpg",
      startHour: DateTime(
          DateTime.now().year, DateTime.now().month, DateTime.now().day, 6, 0),
      endHour: DateTime(
          DateTime.now().year, DateTime.now().month, DateTime.now().day, 15, 0),
      duration: 15),
  Doctor(
      name: "Doctor Ilhan",
      type: "Ear Doctor",
      info:
          "At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga.",
      star: "4.9",
      imageUrl: "img/doctors/doc2.jpg",
      startHour: DateTime(
          DateTime.now().year, DateTime.now().month, DateTime.now().day, 5, 0),
      endHour: DateTime(
          DateTime.now().year, DateTime.now().month, DateTime.now().day, 15, 0),
      duration: 30),................

我想用列表中的信息填充一个小部件,为此,我编写了这段代码:

void initState() {
    super.initState();

    for (var i = 0; i < doctorsList.length; i++) {
      Doctor doctor = doctorsList[i];
      print(doctorsList[i].name);
      bookingCalendarModel = new BookingModel(
          apptName: 'Mock Service',
          apptDuration: doctor.duration,
          bookingEnd: doctor.endHour,
          bookingStart: doctor.startHour);
    }
  }

但是,它不起作用,因为当用户点击一个特定的项目,只有最后一个对象的信息呈现在屏幕上。你能给我一个建议,我该如何解决这个问题?

Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Book now!'),
      ),
      body: Center(
          child: BookingCalendar(
        bookingService: bookingCalendarModel,
        convertStreamResultToDateTimeRanges: convertStreamResultMock,
        getBookingStream: getBookingStreamMock,
        uploadBooking: uploadBookingMock,
        pauseSlots: generatePauseSlots(),
        hideBreakTime: false,
        loadingWidget: const Text('Fetching data...'),
        uploadingWidget: const CircularProgressIndicator(),
        locale: 'en_US',
        startingDayOfWeek: CalendarDays.monday,
        wholeDayIsBookedWidget: const Text('Fully booked! Choose another day'),
        //disabledDates: [DateTime(2023, 1, 20)],
        //disabledDays: [6, 7],
      )),
    );
  }

return Container(
      height: 780,
      child: ListView.builder(
          shrinkWrap: true,
          scrollDirection: Axis.horizontal,
          itemCount: doctorsList.length,
          itemBuilder: (context, index) {
            return Column(
              children: [
                BookingCalendar(
                  bookingService: bookingWidgetList[index],
                  convertStreamResultToDateTimeRanges: convertStreamResultMock,
                  getBookingStream: getBookingStreamMock,
                  uploadBooking: uploadBookingMock,
                  pauseSlots: generatePauseSlots(),
                  hideBreakTime: false,
                  loadingWidget: const Text('Fetching data...'),
                  uploadingWidget: const CircularProgressIndicator(),
                  locale: 'en_US',
                  startingDayOfWeek: CalendarDays.monday,
                  wholeDayIsBookedWidget:
                      const Text('Fully booked! Choose another day'),
                  //disabledDates: [DateTime(2023, 1, 20)],
                  //disabledDays: [6, 7],
                ),
              ],
            );
          }),
    );
kuuvgm7e

kuuvgm7e1#

你应该使用另一个对象列表来转换对象,你的错误是使用一个模型

List<BookingModel> bookingCalendarList= [];
void initState() {
    super.initState();

    for (var i = 0; i < doctorsList.length; i++) {
      Doctor doctor = doctorsList[I]; 
      bookingCalendarList.add(
        BookingModel(
          apptName: 'Mock Service',
          apptDuration: doctor.duration,
          // or this apptDuration: doctorsList[i].duration,
          bookingEnd: doctor.endHour,
          bookingStart: doctor.startHour)
        )
    }
  }

然后可以使用列表视图中的新列表显示在UI中

相关问题