text
stringlengths
1
474
The error message suggests that it’s also related
to box constraints, which are important to understand
to avoid many of the most common Flutter framework errors.What does the error look like?The message shown by the error looks like this:How might you run into the error?This error occurs, for example, when a Row contains a
TextFormField or a TextField but the latter has
no width constraint.
<code_start>Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Unbounded Width of the TextField'),
),
body: const Row(
children: [
TextField(),
],
),
),
);
}<code_end>
How to fix it?As suggested by the error message,
fix this error by constraining the text field
using either an Expanded or SizedBox widget.
The following example demonstrates using an Expanded widget:
<code_start>Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Unbounded Width of the TextField'),
),
body: Row(
children: [
Expanded(child: TextFormField()),
],
),
),
);
}<code_end>
<topic_end>
<topic_start>
‘Incorrect use of ParentData widget’
This error is about missing an expected parent widget.What does the error look like?The message shown by the error looks like this:How might you run into the error?While Flutter’s widgets are generally flexible
in how they can be composed together in a UI,
a small subset of those widgets expect specific parent widgets.
When this expectation can’t be satisfied in your widget tree,
you’re likely to encounter this error.Here is an incomplete list of widgets that expect
specific parent widgets within the Flutter framework.
Feel free to submit a PR (using the doc icon in
the top right corner of the page) to expand this list.How to fix it?The fix should be obvious once you know
which parent widget is missing.<topic_end>
<topic_start>
‘setState called during build’
The build method in your Flutter code isn’t
a good place to call setState,
either directly or indirectly.What does the error look like?When the error occurs,
the following message is displayed in the console:How might you run into the error?In general, this error occurs when the setState
method is called within the build method.A common scenario where this error occurs is when
attempting to trigger a Dialog from within the
build method. This is often motivated by the need to
immediately show information to the user,
but setState should never be called from a build method.The following snippet seems to be a common culprit of this error:
<code_start>Widget build(BuildContext context) {
// Don't do this.
showDialog(
context: context,
builder: (context) {
return const AlertDialog(
title: Text('Alert Dialog'),
);
});
return const Center(
child: Column(
children: <Widget>[
Text('Show Material Dialog'),
],
),
);
}<code_end>
This code doesn’t make an explicit call to setState,
but it’s called by showDialog.
The build method isn’t the right place to call
showDialog because build can be called by the
framework for every frame, for example, during an animation.How to fix it?One way to avoid this error is to use the Navigator API
to trigger the dialog as a route. In the following example,
there are two pages. The second page has a
dialog to be displayed upon entry.
When the user requests the second page by
clicking a button on the first page,
the Navigator pushes two routes–one
for the second page and another for the dialog.
<code_start>class FirstScreen extends StatelessWidget {
const FirstScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('First Screen'),
),
body: Center(
child: ElevatedButton(
child: const Text('Launch screen'),