One-click repair of compilation warnings after Flutter 3 migration

When our app supports flutter 3, both the compilation speed and the running efficiency will be greatly improved in all aspects. But when we compile, there will be warnings like the following.

 1 2 3 
../../../your_pub/lib/src/framework.dart:275:26: Warning: Operand of null-aware operation '!' has type 'SchedulerBinding' which excludes null. [ ] - 'SchedulerBinding' is from 'package:flutter/src/scheduler/binding.dart' ( '../../../code/flutter_3/packages/flutter/lib/src/scheduler/binding.dart' ) . [ ] if ( SchedulerBinding.instance!.schedulerPhase ==

Although the above warning will not affect the compilation of the application, in the long run, it still needs to be solved.

why

The reason is that since flutter 3, SchedulerBinding.instance returns a non-null instance. When we use SchedulerBinding.instance!.schedulerPhase , we will get this warning Warning: Operand of null-aware operation '!' has type 'SchedulerBinding' which excludes null.

How to solve

The solution is very simple, just remove the ! according to the following processing.

 1 
SchedulerBinding.instance.schedulerPhase

What are the scenarios

Starting with flutter3, the following will have compilation warning problems

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 
SchedulerBinding.instance!.xxx SchedulerBinding.instance?.xxx WidgetsBinding.instance!.xxxx WidgetsBinding.instance?.xxxx PaintingBinding.instance?.xxx PaintingBinding.instance!.xxx  RendererBinding.instance!.xxx RendererBinding.instance?.xxxx GestureBinding.instance!.xxx GestureBinding.instance?.xxx

One-click solution

So much content needs to be solved, is there a one-click solution?

If you have been exposed to terminal scripting, the answer is yes. We can handle it using the following shell script.

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 
#!/usr/bin/env bash function sedReplaceFile () { echo $1 sed -i "" -e "s/SchedulerBinding.instance!/SchedulerBinding.instance/g" $1 sed -i "" -e "s/SchedulerBinding.instance?/SchedulerBinding.instance/g" $1 sed -i "" -e "s/WidgetsBinding.instance!/WidgetsBinding.instance/g" $1 sed -i "" -e "s/WidgetsBinding.instance?/WidgetsBinding.instance/g" $1 sed -i "" -e "s/PaintingBinding.instance?/PaintingBinding.instance/g" $1 sed -i "" -e "s/PaintingBinding.instance!/PaintingBinding.instance/g" $1 sed -i "" -e "s/RendererBinding.instance!/RendererBinding.instance/g" $1 sed -i "" -e "s/RendererBinding.instance?/RendererBinding.instance/g" $1 sed -i "" -e "s/GestureBinding.instance!/GestureBinding.instance/g" $1 sed -i "" -e "s/GestureBinding.instance?/GestureBinding.instance/g" $1   } export -f sedReplaceFile find . -name "*.dart" | xargs -I {} bash -c 'sedReplaceFile {}'

implement

 1 2 
cd your_project f3_fix.sh

Note

  • The above script is only verified on the mac system, and Linux may need to make simple modifications by itself.
  • If the three-party pub contains warning problems, you can choose to upgrade the version corresponding to flutter 3.

droidyue_gzh_green_png.png

This article is reprinted from https://droidyue.com/blog/2022/09/12/fix-flutter-3-warnings-quickly/
This site is for inclusion only, and the copyright belongs to the original author.