text stringlengths 1 474 |
|---|
with a single array and a column count. In the implementation, |
the object does not use a linked list like most render objects but |
instead uses an indexable array.The Chip widgets and InputDecoration objects have fields that match |
the slots that exist on the relevant controls. Where a one-size-fits-all |
child model would force semantics to be layered on top of a list of |
children, for example, defining the first child to be the prefix value |
and the second to be the suffix, the dedicated child model allows for |
dedicated named properties to be used instead.This flexibility allows each node in these trees to be manipulated in |
the way most idiomatic for its role. It’s rare to want to insert a cell |
in a table, causing all the other cells to wrap around; similarly, |
it’s rare to want to remove a child from a flex row by index instead |
of by reference.The RenderParagraph object is the most extreme case: it has a child of |
an entirely different type, TextSpan. At the RenderParagraph boundary, |
the RenderObject tree transitions into being a TextSpan tree.The overall approach of specializing APIs to meet the developer’s |
expectations is applied to more than just child models.Some rather trivial widgets exist specifically so that developers |
will find them when looking for a solution to a problem. Adding a |
space to a row or column is easily done once one knows how, using |
the Expanded widget and a zero-sized SizedBox child, but discovering |
that pattern is unnecessary because searching for space |
uncovers the Spacer widget, which uses Expanded and SizedBox directly |
to achieve the effect.Similarly, hiding a widget subtree is easily done by not including the |
widget subtree in the build at all. However, developers typically expect |
there to be a widget to do this, and so the Visibility widget exists |
to wrap this pattern in a trivial reusable widget.<topic_end> |
<topic_start> |
Explicit arguments |
UI frameworks tend to have many properties, such that a developer is |
rarely able to remember the semantic meaning of each constructor |
argument of each class. As Flutter uses the reactive paradigm, |
it is common for build methods in Flutter to have many calls to |
constructors. By leveraging Dart’s support for named arguments, |
Flutter’s API is able to keep such build methods clear and understandable.This pattern is extended to any method with multiple arguments, |
and in particular is extended to any boolean argument, so that isolated |
true or false literals in method calls are always self-documenting. |
Furthermore, to avoid confusion commonly caused by double negatives |
in APIs, boolean arguments and properties are always named in the |
positive form (for example, enabled: true rather than disabled: false).<topic_end> |
<topic_start> |
Paving over pitfalls |
A technique used in a number of places in the Flutter framework is to |
define the API such that error conditions don’t exist. This removes |
entire classes of errors from consideration.For example, interpolation functions allow one or both ends of the |
interpolation to be null, instead of defining that as an error case: |
interpolating between two null values is always null, and interpolating |
from a null value or to a null value is the equivalent of interpolating |
to the zero analog for the given type. This means that developers |
who accidentally pass null to an interpolation function will not hit |
an error case, but will instead get a reasonable result.A more subtle example is in the Flex layout algorithm. The concept of |
this layout is that the space given to the flex render object is |
divided among its children, so the size of the flex should be the |
entirety of the available space. In the original design, providing |
infinite space would fail: it would imply that the flex should be |
infinitely sized, a useless layout configuration. Instead, the API |
was adjusted so that when infinite space is allocated to the flex |
render object, the render object sizes itself to fit the desired |
size of the children, reducing the possible number of error cases.The approach is also used to avoid having constructors that allow |
inconsistent data to be created. For instance, the PointerDownEvent |
constructor does not allow the down property of PointerEvent to |
be set to false (a situation that would be self-contradictory); |
instead, the constructor does not have a parameter for the down |
field and always sets it to true.In general, the approach is to define valid interpretations for all |
values in the input domain. The simplest example is the Color constructor. |
Instead of taking four integers, one for red, one for green, |
one for blue, and one for alpha, each of which could be out of range, |
the default constructor takes a single integer value, and defines |
the meaning of each bit (for example, the bottom eight bits define the |
red component), so that any input value is a valid color value.A more elaborate example is the paintImage() function. This function |
takes eleven arguments, some with quite wide input domains, but they |
have been carefully designed to be mostly orthogonal to each other, |
such that there are very few invalid combinations.<topic_end> |
<topic_start> |
Reporting error cases aggressively |
Not all error conditions can be designed out. For those that remain, |
in debug builds, Flutter generally attempts to catch the errors very |
early and immediately reports them. Asserts are widely used. |
Constructor arguments are sanity checked in detail. Lifecycles are |
monitored and when inconsistencies are detected they immediately |
cause an exception to be thrown.In some cases, this is taken to extremes: for example, when running |
unit tests, regardless of what else the test is doing, every RenderBox |
subclass that is laid out aggressively inspects whether its intrinsic |
sizing methods fulfill the intrinsic sizing contract. This helps catch |
errors in APIs that might otherwise not be exercised.When exceptions are thrown, they include as much information as |
is available. Some of Flutter’s error messages proactively probe the |
associated stack trace to determine the most likely location of the |
actual bug. Others walk the relevant trees to determine the source |
of bad data. The most common errors include detailed instructions |
including in some cases sample code for avoiding the error, or links |
to further documentation.<topic_end> |
<topic_start> |
Reactive paradigm |
Mutable tree-based APIs suffer from a dichotomous access pattern: |
creating the tree’s original state typically uses a very different |
set of operations than subsequent updates. Flutter’s rendering layer |
uses this paradigm, as it is an effective way to maintain a persistent tree, |
which is key for efficient layout and painting. However, it means |
that direct interaction with the rendering layer is awkward at best |
and bug-prone at worst.Flutter’s widget layer introduces a composition mechanism using the |
reactive paradigm7 to manipulate the |
underlying rendering tree. |
This API abstracts out the tree manipulation by combining the tree |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.