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'),
],
);
}
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。