Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

docs.flutter.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.

Learn more

Flutter 3.41 is live! Check out theblog post!

Migrateof to non-nullable return values, and addmaybeOf

To eliminate nullOk parameters to help with API clarity in the face of null safety.

Important

Summary

#

This migration guide describes conversion of code that uses various staticof functions to retrieve information from a context that used to return nullable values, but now return non-nullable values.

Context

#

Flutter has a common pattern of allowing lookup of some types of widgets (typicallyInheritedWidgets, but also others) using static member functions that are typically calledof.

When non-nullability was made the default, it was then desirable to have the most commonly used APIs return a non-nullable value. This is because sayingScrollable.of(context) and then still requiring an! operator or? and a fallback value after that call felt awkward, and was not idiomatic for non-nullable Dart code.

A lot of this migration was performed when we eliminatednullOk parameters in aprevious migration, but someof methods were missed in that migration, and some were subsequently added with nullable return types, counter to our common pattern.

In this migration, the affectedof accessors were split into two calls: one that returned a non-nullable value and threw an exception when the sought-after value was not present (still calledof), and one that returned a nullable value that didn't throw an exception, and returned null if the value was not present (a new method calledmaybeOf).

Description of change

#

The change modified these staticof APIs to return non-nullable values. If a value is not found, they will also now assert in debug mode, and throw an exception in release mode.

This change also introduced new staticmaybeOf APIs alongside the above functions, which return a nullable version of the same value, and simply return null if the value is not found, without throwing any exceptions.

Migration guide

#

To modify your code to use the new form of the APIs, first convert all instances of the original staticof functions (where its nullability is important) to use themaybeOf form instead.

Code before migration:

dart
ScrollController?controller=Scrollable.of(context);

Code after migration:

dart
ScrollController?controller=Scrollable.maybeOf(context);

Then, for instances where the code calls theof API followed by an exclamation point, just remove the exclamation point: it can no longer return a nullable value.

Code before migration:

dart
ScrollControllercontroller=Scrollable.of(context)!;

Code after migration:

dart
ScrollControllercontroller=Scrollable.of(context);

The following can also be helpful:

Timeline

#

In stable release: 3.7

References

#

API documentation:

Relevant PRs:

Was this page's content helpful?

Unless stated otherwise, the documentation on this site reflects Flutter 3.41.2. Page last updated on 2025-10-30.View source orreport an issue.


[8]ページ先頭

©2009-2026 Movatter.jp