Flutter, null safety, learning past examples

Hi, I am trying to learn from looking at past examples but I noticed such past examples always get flagged with null safety issues. Some are obvious fixes, but others are much harder. For example, with key and controllers, like ScrollController. Are there any materials available you can recommend to understand ways to fix null safety problems, for variables that are not obvious?

@paristocode check out:

Really in depth reading on null safety:

As for scroll controller, it really depends on how you initialize it.

class _MyHomeState extends State<MyHome> {
  ScrollController controller;
  List<String> items = List.generate(100, (index) => 'Hello $index');

  @override
  void initState() {
    super.initState();
    controller = ScrollController()..addListener(_scrollListener);
  }

  @override
  void dispose() {
    controller.removeListener(_scrollListener);
    super.dispose();
  }

}

Null safety in this case will say that HiScrollController is not initialized yet! However since you know that you instantiate your scrollController in initState().

You can place the keyword lazy in front of your scrollController property. This let’s dart know that it will be initialized somewhere down the road.

1 Like

Hi.

If you are following a step by step training, it should be great to create 2 projects.

  1. with null safety and recent changes (text buttons…)

  2. without null safety and with deprecated widgets

Step by step corrections instead of global migration is easier for me.

1 Like