Flutter/Dart开发ESP32蓝牙应用时多余Container问题咨询
问题原因及解决方案
触发提示的原因
你代码中isReady为true的分支下的第二个Container没有配置任何额外参数,既没有设置边距、背景、宽高、对齐规则,也没有其他修饰属性。在这种场景下,Container不会产生任何额外的布局效果,只会直接透传展示它的子组件StreamBuilder,这层组件完全没有存在的必要,因此IDE会给出不必要组件的提示。
修复方法
直接删除这层冗余的Container即可,修改后的body部分代码参考如下:
body: Container( child: !isReady ? const Center( child: Text( "Waiting...", style: TextStyle(fontSize: 24, color: Colors.red), ), ) : StreamBuilder<List<int>>( stream: stream, builder: (BuildContext context, AsyncSnapshot<List<int>> snapshot) { if (snapshot.hasError){ return Text('Error: ${snapshot.error}'); } if (snapshot.connectionState == ConnectionState.active) { var currentValue = _dataParser(snapshot.data!); return Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ const Text('Current value', style: TextStyle(fontSize: 14)), Text('${currentValue} jawool', style: const TextStyle( fontWeight: FontWeight.bold, fontSize: 24)) ]) ); } else { return const Text('Check the stream'); } }, ), )
如果你后续需要给数据展示区域加背景色、内边距、圆角等样式,再把这层Container加回来配置对应属性即可。
内容的提问来源于stack exchange,提问作者Josef Göttlicher
相关产品推荐
相关产品推荐

