Need to return a widget in builder: for streambuilder

Advertisements

I am building an appointment screen using the flutter TableCalendar widget inside a streambuilder.

I am getting an error on the "builder:" line saying that it needs to return a widget. If the snapshot has data I am calling _buildTableCalendar which is a widget.

Why is this wrong and how do I fix this?

StreamBuilder<QuerySnapshot>(
            stream: _db
                .collection('users')
                .doc(ref.read(globalsNotifierProvider).currentUserId)
                .collection('events')
                .where('eventDate', isGreaterThanOrEqualTo: kFirstDay)
                .where('eventDate', isLessThanOrEqualTo: kLastDay)
                .snapshots(),
            builder: (BuildContext context, AsyncSnapshot snapshot) { <<< ERROR
              debugPrint('Data: ${snapshot.data}');
              if (snapshot.connectionState == ConnectionState.waiting) {
                return const CircularProgressIndicator();
              } else {
                // This is in the events provider file
                List<Events> eventsList =
                    snapshot.data!.docs.map((e) => Events.fromJson(e)).toList();
                
                if (snapshot.hasData && snapshot.data != null) {
                  return _buildTableCalendar(eventsList); <<< This is a widget
                }
              }
            },
          ),

This is the code for _buildTableCalendar which is a widget:

Widget _buildTableCalendar([List<Events>? events, List<Trxn>? trxns]) {
    return TableCalendar(
      firstDay: kFirstDay,
      lastDay: kLastDay,
      focusedDay: _focusedDay,
      selectedDayPredicate: (day) => isSameDay(_selectedDay, day),
      locale: 'en_US',
      eventLoader: (day) {
        return _getEventsForDay(day, events, trxns);
      },
      startingDayOfWeek: StartingDayOfWeek.sunday,
      calendarStyle: CalendarStyle(
        isTodayHighlighted: true,
        selectedDecoration: BoxDecoration(color: Colors.deepOrange[400]),
        todayDecoration: BoxDecoration(color: Colors.deepOrange[200]),
        markerDecoration: const BoxDecoration(color: Colors.deepPurpleAccent),
        outsideDaysVisible: false,
      ),
      headerStyle: HeaderStyle(
        formatButtonTextStyle:
            const TextStyle().copyWith(color: Colors.white, fontSize: 15.0),
        formatButtonDecoration: BoxDecoration(
          color: Colors.deepOrange[400],
          borderRadius: BorderRadius.circular(16.0),
        ),
      ),
      onDaySelected: (selectedDay, focusedDay) {
        setState(() {
          _selectedDay = selectedDay;
          _focusedDay = focusedDay; // update `_focusedDay` here as well
        });
      },
      onPageChanged: (focusedDay) {
        _focusedDay = focusedDay;
      },
    );
  }

>Solution :

You need an "else" that returns a widget for when this case is false:

if (snapshot.hasData && snapshot.data != null) {

You’re not permitted to not return a Widget. The error message is telling you how to fix it.

In other words, what do you want to show if there’s no data?

Leave a ReplyCancel reply