[Breaking Change] Change of several methods in BuildContext

283 views
Skip to first unread message

Alexandre Ardhuin

unread,
Nov 14, 2019, 8:19:29 PM11/14/19
to flutter-...@googlegroups.com
We propose to make the following changes to BuildContext methods:

OldNew
InheritedWidget inheritFromElement(InheritedElement ancestor, { Object aspect })InheritedWidget dependOnInheritedElement(InheritedElement ancestor, { Object aspect })
InheritedWidget inheritFromWidgetOfExactType(Type targetType, { Object aspect })T dependOnInheritedWidgetOfExactType<T extends InheritedWidget>({ Object aspect })
InheritedElement ancestorInheritedElementForWidgetOfExactType(Type targetType)InheritedElement getElementForInheritedWidgetOfExactType<T extends InheritedWidget>()
Widget ancestorWidgetOfExactType(Type targetType)T findAncestorWidgetOfExactType<T extends Widget>()
State ancestorStateOfType(TypeMatcher matcher)T findAncestorStateOfType<T extends State>()
State rootAncestorStateOfType(TypeMatcher matcher)T findRootAncestorStateOfType<T extends State>()
RenderObject ancestorRenderObjectOfType(TypeMatcher matcher)T findAncestorRenderObjectOfType<T extends RenderObject>()

The old methods were introduced at a time were methods couldn't be generics. Making them generics today allows better type safety and will avoid explicit downcast with the upcoming NNBD.
We've also renamed those methods to have more consistent and explicit names.

This change is being staged here : https://github.com/flutter/flutter/pull/44189

If you face some errors the changes should be straightforward:


OldNew
c.inheritFromElement(myElement)c.dependOnInheritedElement(myElement)
c.inheritFromWidgetOfExactType(MyWidget)c.dependOnInheritedWidgetOfExactType<MyWidget>()
c.ancestorInheritedElementForWidgetOfExactType(MyWidget)c.getElementForInheritedWidgetOfExactType<MyWidget>()
c.ancestorWidgetOfExactType(MyWidget)c.findAncestorWidgetOfExactType<MyWidget>()
c.ancestorStateOfType(const TypeMatcher<MyState>())c.findAncestorStateOfType<MyState>()
c.rootAncestorStateOfType(const TypeMatcher<MyState>())c.findRootAncestorStateOfType<MyState>()
c.ancestorRenderObjectOfType(const TypeMatcher<MyRenderObject>())c.findAncestorRenderObjectOfType<MyRenderObject>()


Moreover, the TypeMatcher class was used only for some of those deprecated methods. So it will also be deprecated and will be removed afterwards.

If you have any questions, comments, or concerns please let us know on the PR!

Cheers,
Alexandre
Reply all
Reply to author
Forward
0 new messages