Okanlawon Gideon
Okanlawon Gideon

Reputation: 19

Using Stream Builder and for loop error in flutter

This is suppose to update scream using a stream builder but i keep getting: The following _TypeError was thrown building StreamBuilder(dirty, state: _StreamBuilderBaseState<dynamic, AsyncSnapshot>#e186e): type 'String' is not a subtype of type 'Iterable'

Please what am i getting wrong the error occured when i used the for loop in the code.

class CheckInUpdateLogCard extends StatefulWidget {
  const CheckInUpdateLogCard({super.key});

  @override
  State<CheckInUpdateLogCard> createState() => _CheckInUpdateLogCardState();
}

class _CheckInUpdateLogCardState extends State<CheckInUpdateLogCard> {
  late Stream activityTextStream;

  @override
  void initState() {
    super.initState();
    activityTextStream = getActivityText();
  }

  Stream<String> getActivityText() async* {
    await Future.delayed(const Duration(seconds: 4));
    yield 'Cusco';

    await Future.delayed(const Duration(seconds: 1));
    yield 'Salvador';
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 500,
      width: double.infinity,
      padding: const EdgeInsets.all(20),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(20),
        color: Colors.white,
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          const ApartmentText(
            theText: 'Recent Activities',
            theColor: mainColor,
            theSize: 24,
          ),
          kSpacerHeight20,
          StreamBuilder(
              stream: activityTextStream,
              builder: (context, snapshot) {
                if (snapshot.connectionState == ConnectionState.waiting) {
                  return const Text('Loading please wait...');
                } else if (snapshot.hasData) {
                  final Iterable textData = snapshot.data;
                  List<Widget> returnWidgets = [];
                  for (var theTextWidget in textData) {
                    final messageWidget =
                        ActivitiesText(theText: '$theTextWidget');
                    returnWidgets.add(messageWidget);
                  }
                  return Column(
                    children: [...returnWidgets],
                  );
                } else {
                  return const Text('No Data');
                }
              })
        ],
      ),
    );
  }
}

The code is surppose to return a text with stream builder

Upvotes: 0

Views: 36

Answers (1)

Okanlawon Gideon
Okanlawon Gideon

Reputation: 19

I think you're confusing what a stream does, your stream return String (check your return Stream from your method getActivityText) , you should yield a List if that's what you want – Thanks to EdwynZN

Upvotes: 0

Related Questions