avoid_ unnecessary_ containers
Details about the 'avoid_unnecessary_containers' diagnostic produced by the Dart analyzer.
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:
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:
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:
import 'package:flutter/material.dart';
Widget buildRow() {
return Row(children: [Text('a'), Text('b')]);
}
Unless stated otherwise, the documentation on this site reflects Dart 3.12.2. Report an issue.