【问题标题】:type 'String' is not a subtype of type 'int' of 'index' error Flutter类型'String'不是'index'错误Flutter的'int'类型的子类型
【发布时间】:2021-05-10 08:36:50
【问题描述】:

这是我目前正在使用的颤振日历小部件的代码:

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' 错误。如果有人能够发现任何问题,那就太好了!

【问题讨论】:

    标签: flutter dart


    【解决方案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']);
    

    【讨论】:

      【解决方案2】:

      你应该试试:

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

      【讨论】:

      • 不幸的是,它仍然给我错误,不过非常感谢!
      猜你喜欢
      • 2020-04-22
      • 2022-09-27
      • 2020-10-02
      • 1970-01-01
      • 2019-04-24
      • 2021-03-29
      • 1970-01-01
      • 2020-04-23
      • 1970-01-01
      相关资源
      最近更新 更多