Cannot read property 'getPositionForOffset$1' of null

Issue

There is a bug in my flutter web app which shown up very rarely after changing the app localization at runtime, this dynamic behavior implemented using provider and flutter_localizations packages as the official docs described here and changes the locale and darkTheme > fontFamily property of MaterialApp.

This is the stack-trace using flutter build web --profile --dart-define=Dart2jsOptimization=O0

Uncaught TypeError: Cannot read property 'getPositionForOffset$1' of null
    at RenderParagraph.hitTestChildren$2$position (main.dart.js:96010)
    at RenderParagraph.hitTest$2$position (main.dart.js:90914)
    at RenderShiftedBox_hitTestChildren_closure.call$2 (main.dart.js:98256)
    at BoxHitTestResult.addWithPaintOffset$3$hitTest$offset$position (main.dart.js:90784)
    at _RenderAppBarTitleBox.hitTestChildren$2$position (main.dart.js:98249)
    at _RenderAppBarTitleBox.hitTest$2$position (main.dart.js:90914)
    at RenderSemanticsAnnotations.hitTestChildren$2$position (main.dart.js:96532)
    at RenderSemanticsAnnotations.hitTest$2$position (main.dart.js:90914)
    at RenderBoxContainerDefaultsMixin_defaultHitTestChildren_closure.call$2 (main.dart.js:91033)
    at BoxHitTestResult.addWithPaintOffset$3$hitTest$offset$position (main.dart.js:90784)

OR

Uncaught TypeError: Cannot read property 'getPositionForOffset$1' of null
    at RenderParagraph.hitTestChildren$2$position (main.dart.js:96020)
    at RenderParagraph.hitTest$2$position (main.dart.js:90924)
    at RenderShiftedBox_hitTestChildren_closure.call$2 (main.dart.js:98266)
    at BoxHitTestResult.addWithPaintOffset$3$hitTest$offset$position (main.dart.js:90794)
    at RenderPositionedBox.hitTestChildren$2$position (main.dart.js:98259)
    at RenderPositionedBox.hitTest$2$position (main.dart.js:90924)
    at RenderConstrainedBox.hitTestChildren$2$position (main.dart.js:96542)
    at RenderConstrainedBox.hitTest$2$position (main.dart.js:90924)
    at RenderExcludeSemantics.hitTestChildren$2$position (main.dart.js:96542)
    at RenderExcludeSemantics.hitTest$2$position

Any help would be greatly appreciated

Solution

The solution is to provide a UniqueKey() to MaterialApp() when the top most widget tree is going to rebuild after a localization change requested, because the previous calculated states or constraints are not necessarily compatible with the new fontFamily or locale. Also note that there might be another more efficient solution to this problem but this will do the job.

Answered By – HoseinGhanbari

Answer Checked By – Terry (FlutterFixes Volunteer)

Leave a Reply

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