若变量不为空,则将空值赋给数组



我正在制作一个上传文件的应用程序。这些文件收集在一个数组中。只能有3个文件。登录时,数据来自数据库。如果已经有一个值,那么索引处的值一定已经被一个空值占用了。现在我有一个错误-类型"String"不是类型转换中类型"Widget"的子类型

String edit_doc1 = globals.currentUser.userInfo['doc1'];
String edit_doc2 = globals.currentUser.userInfo['doc2'];
String edit_doc3 = globals.currentUser.userInfo['doc3'];
class _EditAccountScreenState extends State<EditAccountScreen> {
List<Widget> fileListThumb;
List<File> fileList = new List<File>();
Future pickFiles() async{
List<Widget> thumbs = new List<Widget>();
fileListThumb.forEach((element) {
thumbs.add(element);
});
await FilePicker.getMultiFile(
type: FileType.custom,
allowedExtensions: ['jpg', 'jpeg', 'bmp', 'pdf', 'doc', 'docx'],
).then((files){
if(files != null && files.length>0){
files.forEach((element) {
List<String> picExt = ['.jpg', '.jpeg', '.bmp'];
if(picExt.contains(extension(element.path))){
thumbs.add(Padding(
padding: EdgeInsets.all(1),
child:new Image.file(element)
)
);
}
else
thumbs.add( Container(
child : Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children:<Widget>[
Icon(Icons.insert_drive_file),
Text(extension(element.path))
]
)
));
fileList.add(element);
});
setState(() {
fileListThumb = thumbs;
print(fileListThumb.length);
});
}
});
}
@override
Widget build(BuildContext context) {
if(fileListThumb == null)
fileListThumb = [
InkWell(
onTap: pickFiles,
child: Container(
// alignment: Alignment.center,
// height: 50,
// width: 90,
child : Icon(Icons.add),
// child : Text('Загрузить файл', textAlign: TextAlign.center,),
decoration: new BoxDecoration(
borderRadius: new BorderRadius.circular(16.0),
color: Colors.green,
),
),
)
];
if(fileListThumb.length == 4) {
fileListThumb.removeAt(0);
}
if(editGlobals.edit_doc1 != '') {
fileListThumb[1] = '' as Widget;
}
if(editGlobals.edit_doc2 != '') {
fileListThumb[2] = '' as Widget;
}
if(editGlobals.edit_doc3 != '') {
fileListThumb[3] = '' as Widget;
}

您的错误很明显:

fileListThumb[1] = '' as Widget;

您尝试将''(一个字符串(分配给WidgetfileListThumb[1],这是不可能的。如果要从列表中清除项目,可以将其设置为null或像使用fileListThumb.removeAt(0);一样将其删除。

PS:请注意数组从0索引开始,而不是从1开始。

最新更新