StatelessComponent class abstract

A component that does not require mutable state.

A stateless component is a component that describes part of the user interface by building a constellation of other components that describe the user interface more concretely. The building process continues recursively until the description of the user interface is fully concrete (e.g., consists entirely of DOMComponents, which describe concrete DOM elements).

Stateless component are useful when the part of the user interface you are describing does not depend on anything other than the configuration information in the object itself and the BuildContext in which the component is inflated. For compositions that can change dynamically, e.g. due to having an internal clock-driven state, or depending on some system state, consider using StatefulComponent.

Performance considerations

The build method of a stateless component is typically only called in three situations: the first time the component is inserted in the tree, when the component's parent changes its configuration, and when an InheritedComponent it depends on changes.

If a component's parent will regularly change the component's configuration, or if it depends on inherited components that frequently change, then it is important to optimize the performance of the build method to maintain a fluid rendering performance.

There are several techniques one can use to minimize the impact of rebuilding a stateless component:

  • Minimize the number of nodes transitively created by the build method and any components it creates.

  • Use const components where possible, and provide a const constructor for the component so that users of the component can also do so.

  • When trying to create a reusable piece of UI, prefer using a component rather than a helper method. For example, if there was a function used to build a component, a State.setState call would require Flutter to entirely rebuild the returned wrapping component. If a Component was used instead, we would be able to efficiently re-render only those parts that really need to be updated. Even better, if the created component is const, we would short-circuit most of the rebuild work.

  • Consider refactoring the stateless component into a stateful component so that it can use some of the techniques described at StatefulComponent, such as caching common parts of subtrees and using GlobalKeys when changing the tree structure.

  • If the component is likely to get rebuilt frequently due to the use of InheritedComponents, consider refactoring the stateless component into multiple components, with the parts of the tree that change being pushed to the leaves. For example instead of building a tree with four components, the inner-most component depending on some InheritedComponent, consider factoring out the part of the build function that builds the inner-most component into its own component, so that only the inner-most component needs to be rebuilt when the inherited component changes.

By convention, component constructors only use named arguments. Also by convention, the first argument is key, and the last argument is child, children, or the equivalent.

See also:

  • StatefulComponent and State, for components that can build differently several times over their lifetime.
  • InheritedComponent, for components that introduce ambient state that can be read by descendant components.
Inheritance
Implementers

Constructors

StatelessComponent({Key? key})
Initializes key for subclasses.
const

Properties

hashCode int
The hash code for this object.
no setterinherited
key Key?
Controls how one component replaces another component in the tree.
finalinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

build(BuildContext context) Iterable<Component>
Describes the part of the user interface represented by this component.
createElement() Element
Creates a StatelessElement to manage this component's location in the tree.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited