Tom
Tom

Reputation: 113

if variable is not empty, assign empty value to array

I am making an application to upload files. The files are collected in an array. There can only be 3 files. When you login, data comes from the database. And if there is already a value, then the value at the index must already be occupied by an empty value. Now I have an error - type 'String' is not a subtype of type 'Widget' in type cast

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;
    }

Upvotes: 0

Views: 345

Answers (1)

BabC
BabC

Reputation: 1084

Your error is clear :

fileListThumb[1] = '' as Widget;

You try to assign '', which is a string, to a Widget, fileListThumb[1] which is not possible. If you want to clear an item fro ma list, you can set it to null or remove it like you did with fileListThumb.removeAt(0);.

PS : Pay attention that array starts at 0 index, not 1.

Upvotes: 2

Related Questions