'String'类型不是'index'错误Flutter的'int'类型的子类型

时间:2021-02-06 01:47:37

标签: flutter dart

这是我目前使用的 Flutter 日历小部件的代码:

Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("Calendar"),
        ),
        body: StreamBuilder(
            stream: FirebaseFirestore.instance.collection('Events').snapshots(),
            builder: (context, snapshot) {
              if (!snapshot.hasData) return Text('Loading data... Please Wait');
              print(snapshot.data.documents['name']);
              return Column(
                mainAxisSize: MainAxisSize.max,
                children: <Widget>[
                  _buildTableCalendar(context, snapshot),
                  const SizedBox(height: 8.0),
                  Expanded(child: _buildEventList()),
                ],
              );
            }));
  }

  Widget _buildTableCalendar(BuildContext context, AsyncSnapshot snapshot) {

    return TableCalendar(
      calendarController: _controller,
      events: _events,
      holidays: _holidays,
      startingDayOfWeek: StartingDayOfWeek.monday,
      calendarStyle: CalendarStyle(
        selectedColor: Colors.purple[400],
        todayColor: Colors.purple[200],
        markersColor: Colors.purple[700],
        outsideDaysVisible: false,
      ),
      headerStyle: HeaderStyle(
        formatButtonTextStyle:
            TextStyle().copyWith(color: Colors.white, fontSize: 15.0),
        formatButtonDecoration: BoxDecoration(
          color: Colors.purple[400],
          borderRadius: BorderRadius.circular(16.0),
        ),
      ),
      onDaySelected: _onDaySelected,
      onVisibleDaysChanged: _onVisibleDaysChanged,
      onCalendarCreated: _onCalendarCreated,
    );
  }

我目前只是想打印快照的内容,因为我不太确定如何将事件信息输入到代码中,但我一直收到 type 'String' is not a subtype of type 'int' of 'index' 错误。如果有人能够发现任何问题,那就太好了!

2 个答案:

答案 0 :(得分:1)

试试这个;

Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("Calendar"),
        ),
        body: StreamBuilder(
            stream: FirebaseFirestore.instance.collection('Events').snapshots(),
            builder: (context, snapshot) {
              if (!snapshot.hasData) return Text('Loading data... Please Wait');
              print(snapshot.data.documents[0]['name']); ///here
              return Column(
                mainAxisSize: MainAxisSize.max,
                children: <Widget>[
                  _buildTableCalendar(context, snapshot),
                  const SizedBox(height: 8.0),
                  Expanded(child: _buildEventList()),
                ],
              );
            }));
  }

  Widget _buildTableCalendar(BuildContext context, AsyncSnapshot snapshot) {

    return TableCalendar(
      calendarController: _controller,
      events: _events,
      holidays: _holidays,
      startingDayOfWeek: StartingDayOfWeek.monday,
      calendarStyle: CalendarStyle(
        selectedColor: Colors.purple[400],
        todayColor: Colors.purple[200],
        markersColor: Colors.purple[700],
        outsideDaysVisible: false,
      ),
      headerStyle: HeaderStyle(
        formatButtonTextStyle:
            TextStyle().copyWith(color: Colors.white, fontSize: 15.0),
        formatButtonDecoration: BoxDecoration(
          color: Colors.purple[400],
          borderRadius: BorderRadius.circular(16.0),
        ),
      ),
      onDaySelected: _onDaySelected,
      onVisibleDaysChanged: _onVisibleDaysChanged,
      onCalendarCreated: _onCalendarCreated,
    );
  }

我更改了打印语句

print(snapshot.data.documents[0]['name']);

答案 1 :(得分:0)

你应该尝试:

print(snapshot.data.documents['name'].get().toString());