4

I have a statefull widget

class Period extends StatefulWidget{
  final StreamController<List<dynamic>> notify = StreamController<List<dynamic>>();
  final int period;
  Period(List<dynamic> data, this.period){
    notify.sink.add(data);
    print("created new Period:");
    print(period);
  }

  void dispose() {
    notify.close();
  }
  @override
  _PeriodState createState() => _PeriodState();
}

class _PeriodState extends State<Period> {

  bool isNull = true;
  bool isListening = false;
  List<Widget> lessons;

  _PeriodState(){
    lessons = [(genTime())];
    widget.notify.stream.listen(update);
    isListening = true;
  }
}

But on the line widget.notify.stream.listen(update); it catches the exception "The getter 'notify' was called on null." Why would widget be null? I print out the List the Periods are part of, but all of them are initialized properly.

1 Answer 1

8

Don't use the constructor. Instead use initState

class Foo extends State<Bar> {
  @override
  void initState() {
    // widget is not null here
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Why the widget is null in the constructor? :0

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.