Flutter 如何正确地将小部件存储在列表中以返回小部件类
我是新来的。我知道在Widget类中,您只能返回一次。我正在使用For循环,希望将其添加到列表中,以便返回它 我的代码:Flutter 如何正确地将小部件存储在列表中以返回小部件类,flutter,dart,Flutter,Dart,我是新来的。我知道在Widget类中,您只能返回一次。我正在使用For循环,希望将其添加到列表中,以便返回它 我的代码: Widget _blacklistInfos() { Map<String, dynamic> mapScan = jsonDecode(scanRaw); var widgetsList = List<Widget>(); if (scanRaw.contains('{"BLACKLIST":{"INFO":[
Widget _blacklistInfos() {
Map<String, dynamic> mapScan = jsonDecode(scanRaw);
var widgetsList = List<Widget>();
if (scanRaw.contains('{"BLACKLIST":{"INFO":[')) {
for (int x = 0; x < mapScan['BLACKLIST']['INFO'].length; x++) {
print('a');
widgetsList.add(
Card(
child: ListTile(
leading: CircleAvatar(
backgroundColor: Colors.lightBlue,
child: Icon(Icons.check_circle, color: Colors.white),
),
title: Text(mapScan['BLACKLIST']['INFO'][x][0]),
subtitle: Text(mapScan['BLACKLIST']['INFO'][x][1]),
trailing: Icon(Icons.arrow_forward),
onTap: () async {
await launch(mapScan['BLACKLIST']['INFO'][x][1]);
},
),
),
);
}
return ListView(children: widgetsList);
} else {
return SizedBox.shrink();
}
}
Widget\u blacklistInfos(){
Map mapScan=jsonDecode(scanRaw);
var widgetsList=List();
if(scanRaw.contains('{“黑名单”:{“信息”:[')){
对于(int x=0;x
是否有任何方法可以正确地执行此操作
提前谢谢。还不错。我将应用以下更改:
- 为了可读性(和安全性),请使用“地图”而不是“For”,如下所示:
var blackListEntries=mapScan['BLACKLIST']['INFO'].map((条目)=>Card(…)
- 可以省略
-您也可以只返回一个包含空子列表的ListView返回SizedBox.shrink();