avoid_unnecessary_containers
Unnecessary instance of 'Container'.
Description
#The analyzer produces this diagnostic when a widget tree contains an instance of Container
and the only argument to the constructor is child:
.
Example
#The following code produces this diagnostic because the invocation of the Container
constructor only has a child:
argument:
dart
import 'package:flutter/material.dart';
Widget buildRow() {
return Container(
child: Row(
children: [
Text('a'),
Text('b'),
],
)
);
}
Common fixes
#If you intended to provide other arguments to the constructor, then add them:
dart
import 'package:flutter/material.dart';
Widget buildRow() {
return Container(
color: Colors.red.shade100,
child: Row(
children: [
Text('a'),
Text('b'),
],
)
);
}
If no other arguments are needed, then unwrap the child widget:
dart
import 'package:flutter/material.dart';
Widget buildRow() {
return Row(
children: [
Text('a'),
Text('b'),
],
);
}
Was this page's content helpful?
Thank you for your feedback!
Provide details Thank you for your feedback! Please let us know what we can do to improve.
Provide details Unless stated otherwise, the documentation on this site reflects Dart 3.8.1. Page last updated on 2025-05-08. View source or report an issue.