OpenFlutter / flutter_screenutil

Flutter screen adaptation, font adaptation, get screen information
https://pub.dartlang.org/packages/flutter_screenutil
Apache License 2.0
3.87k stars 493 forks source link

Bugfix on flutter_screenutil: ^5.5.4 #435

Closed definitelyme closed 1 year ago

definitelyme commented 1 year ago

From version 5.5.4 the below exception is thrown while building ScreenUtilInit

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
dependOnInheritedWidgetOfExactType<MediaQuery>() or dependOnInheritedElement() was called before
_ScreenUtilInitState.initState() completed.
When an inherited widget changes, for example if the value of Theme.of() changes, its dependent
widgets are rebuilt. If the dependent widget's reference to the inherited widget is in a constructor
or an initState() method, then the rebuilt dependent widget will not reflect the changes in the
inherited widget.
Typically references to inherited widgets should occur in widget build() methods. Alternatively,
initialization based on inherited widgets can be placed in the didChangeDependencies method, which
is called after initState and whenever the dependencies change thereafter.

This PR should resolve issue #434 by ensuring calls to access MediaQueryData happen in didChangeDependencies or build method. If you have better alternatives, do let me know.

definitelyme commented 1 year ago

For anyone interested, you can use this temporary solution in your pubspec.yaml.

flutter_screenutil:
    git:
      url: https://github.com/Flutter-Fox/flutter_screenutil.git
      ref: fix-assertion