首页 > 解决方案 > Flutter 将 Firestore 数组值返回到列表

问题描述

我正在使用 Flutter 表日历插件制作日历。为了将事件放入日历,我必须将数据添加到 _events 地图。我想从 Firestore 文档中获取数据,并将数据放入 _events 映射中。但是,我不知道该怎么做。我到处搜索,但我无法得到答案。

这是我的代码

class _MemberEventsState extends State<MemberEvents>
    with TickerProviderStateMixin {
  Map<DateTime, List> _events;
  List _selectedEvents;
  AnimationController _animationController;
  CalendarController _calendarController;
  List<String> list = List();

  @override
  void initState() {
    super.initState();
    final _selectedDay = DateTime.now();

    Firestore.instance
        .collection('events')
        .document('2019-07-30')
        .get()
        .then((DocumentSnapshot ds) {
      list = List.from(ds['title']);
    });

    _events = {DateTime.parse("2019-08-01"): list};
    _selectedEvents = _events[_selectedDay] ?? [];
    _calendarController = CalendarController();
    _animationController = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 400),
    );
    _animationController.forward();
  }

  @override
  void dispose() {
    _animationController.dispose();
    _calendarController.dispose();
    super.dispose();
  }

  void _onDaySelected(DateTime day, List events) {
    print('CALLBACK: _onDaySelected');
    setState(() {
      _selectedEvents = events;
    });
  }

  void _onVisibleDaysChanged(
      DateTime first, DateTime last, CalendarFormat format) {
    print('CALLBACK: _onVisibleDaysChanged');
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: Column(
        mainAxisSize: MainAxisSize.max,
        children: <Widget>[
          _buildTableCalendar(),
          const SizedBox(height: 8.0),
          const SizedBox(height: 8.0),
          Expanded(child: _buildEventList()),
        ],
      ),
    );
  }

  Widget _buildTableCalendar() {
    return TableCalendar(
      calendarController: _calendarController,
      events: _events,
      startingDayOfWeek: StartingDayOfWeek.sunday,
      calendarStyle: CalendarStyle(
        selectedColor: Colors.deepOrange[400],
        todayColor: Colors.blueAccent[200],
        markersColor: Colors.brown[700],
        outsideDaysVisible: false,
      ),
      headerStyle: HeaderStyle(
        formatButtonTextStyle:
            TextStyle().copyWith(color: Colors.white, fontSize: 15.0),
        formatButtonDecoration: BoxDecoration(
          color: Colors.deepOrange[400],
          borderRadius: BorderRadius.circular(16.0),
        ),
      ),
      onDaySelected: _onDaySelected,
      onVisibleDaysChanged: _onVisibleDaysChanged,
    );
  }

  Widget _buildEventList() {
    return ListView(
      children: _selectedEvents
          .map((event) => Container(
                decoration: BoxDecoration(
                  border: Border.all(width: 0.8),
                  borderRadius: BorderRadius.circular(12.0),
                ),
                margin:
                    const EdgeInsets.symmetric(horizontal: 8.0, vertical: 4.0),
                child: ListTile(
                  title: Text(event.toString()),
                ),
              ))
          .toList(),
    );
  }
}

因此,在实现目标的第一步中,我制作了一个名为 2019-07-30 的文档,然后在其中制作了一个名为 title 的数组。然后我尝试将数组中的值获取到名为列表的列表中。但是,列表返回 null。

我不知道我哪里错了。

我是 Flutter 的新手,所以这个问题可能看起来很愚蠢。

另外,我是stackoverflow的新手,所以如果我在描述问题时做错了任何步骤,请告诉我,以便我修复它。

这是我的 Firestore 数据

标签: listfirebasefluttergoogle-cloud-firestore

解决方案


推荐阅读