# TextField requires a MaterialLocalizations widget

> TextField now throws an assert error if there is no MaterialLocalizations widget in the widget tree.




:::important
These breaking change docs are accurate, as of the release
under which they are published. Over time, the
workarounds described here might become inaccurate.
We don't, in general, keep these breaking change docs up
to date as of each release.

这些破坏性改动文档的准确性仅限于其发布时对应的版本。
随着时间的推移，文档中描述的方案可能会逐渐失效。
通常情况下，我们不会在每次发布新版本时都对这些文档进行同步更新。

The [breaking change index file](/release/breaking-changes)
lists the docs created for each release.

[破坏性改动列表](/release/breaking-changes) 
列出了每个版本的文档。

:::


## Summary

Instances of `TextField` must have a
`MaterialLocalizations` present in the widget tree.
Trying to instantiate a `TextField` without the proper localizations
results in an assertion such as the following:

```plaintext
No MaterialLocalizations found.
TextField widgets require MaterialLocalizations to be provided by a Localizations widget ancestor.
The material library uses Localizations to generate messages, labels, and abbreviations.
To introduce a MaterialLocalizations, either use a MaterialApp at the root of your application to
include them automatically, or add a Localization widget with a MaterialLocalizations delegate.
The specific widget that could not find a MaterialLocalizations ancestor was:
  TextField
```

## Context

If the `TextField` descends from a `MaterialApp`, the
`DefaultMaterialLocalizations` is already instantiated
and won't require any changes to your existing code.

If the `TextField` doesn't descend from `MaterialApp`,
you can use a `Localizations` widget to
provide your own localizations.

## Migration guide

If you see an assertion error, make sure that
locale information is available to the `TextField`,
either through an ancestor `MaterialApp`
(that automatically provides `Localizations`), or
by creating your own `Localizations` widget.

Code before migration:

```dart
import 'package:flutter/material.dart';

void main() => runApp(Foo());

class Foo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MediaQuery(
      data: const MediaQueryData(),
      child: Directionality(
        textDirection: TextDirection.ltr,
        child: Material(
          child: TextField(),
        ),
      ),
    );
  }
}
```

Code after migration (Providing localizations using the `MaterialApp`):

```dart
import 'package:flutter/material.dart';

void main() => runApp(Foo());

class Foo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Material(
        child: TextField(),
      ),
    );
  }
}
```

Code after migration (Providing localizations via the `Localizations` widget):

```dart
import 'package:flutter/material.dart';

void main() => runApp(Foo());

class Foo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Localizations(
      locale: const Locale('en', 'US'),
      delegates: const <LocalizationsDelegate<dynamic>>[
        DefaultWidgetsLocalizations.delegate,
        DefaultMaterialLocalizations.delegate,
      ],
      child: MediaQuery(
        data: const MediaQueryData(),
        child: Directionality(
          textDirection: TextDirection.ltr,
          child: Material(
            child: TextField(),
          ),
        ),
      ),
    );
  }
}
```

## Timeline

Landed in version: 1.20.0-1.0.pre<br>
In stable release: 1.20

## References

API documentation:

* [`TextField`][]
* [`Localizations`][]
* [`MaterialLocalizations`][]
* [`DefaultMaterialLocalizations`][]
* [`MaterialApp`][]
* [Internationalizing Flutter apps][]

Relevant PR:

* [PR 58831: Assert debugCheckHasMaterialLocalizations on TextField][]

[`TextField`]: https://api.flutter-io.cn/flutter/material/TextField-class.html
[`Localizations`]: https://api.flutter-io.cn/flutter/widgets/Localizations-class.html
[`MaterialLocalizations`]: https://api.flutter-io.cn/flutter/material/MaterialLocalizations-class.html
[`DefaultMaterialLocalizations`]: https://api.flutter-io.cn/flutter/material/DefaultMaterialLocalizations-class.html
[`MaterialApp`]: https://api.flutter-io.cn/flutter/material/MaterialApp-class.html
[Internationalizing Flutter apps]: /ui/internationalization
[PR 58831: Assert debugCheckHasMaterialLocalizations on TextField]: https://github.com/flutter/flutter/pull/58831

