1

This is the code from a flutter calendar widget that I am currently using:

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,
    );
  }

I am currently just trying to print the contents of snapshot, as I am not too sure how I will get the event information into the code, but I keep getting the type 'String' is not a subtype of type 'int' of 'index' error. If anyone would be able to identify any issues, that would be great!

2 Answers 2

2

try this;

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,
    );
  }

I changed the print statement

print(snapshot.data.documents[0]['name']);
Sign up to request clarification or add additional context in comments.

Comments

0

You should try:

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

1 Comment

Unfortunately, it's still giving me the error, thank you so much though!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.