The code I wrote about dart stream gives me a null value whenever I work in the terminal

Issue

Every time I run the code I wrote, the terminal does not return a value to me. I did not understand problem

   import 'dart:async';

main() {
  functionforStreamController();

  myStreamController.stream.listen((e) => print("Selam"));
}

StreamController myStreamController = StreamController();

functionforStreamController() async* {
  for (int i = 0; i <= 10; i++) {
    await Future.delayed(Duration(seconds: 1));
    myStreamController.sink.add(i);
  }
}

Solution

try this, remove asterisk*

import 'dart:async';


StreamController myStreamController = StreamController();

main() {
  myStreamController.stream.listen((e) => print("Selam"));
  functionforStreamController();
}


functionforStreamController() async {
  print("functionforStreamController");
  for (int i = 0; i <= 10; i++) {
    await Future.delayed(Duration(seconds: 1));
    myStreamController.sink.add(i);
  }
}

Answered By – Lakhwinder Singh

Answer Checked By – Marilyn (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *