Home > Software engineering >  Flutter - File() constructor "The argument type 'String' can't be assigned to th
Flutter - File() constructor "The argument type 'String' can't be assigned to th

Time:02-10

I'm new to flutter and trying to send image via chat section. I'm getting 2 errors in my code. how can I solve this. appreciate your help on this.

  • error: The argument type 'String' can't be assigned to the parameter type 'List'.

  • error: 2 positional argument(s) expected, but 1 found. )

enter image description here

final FirebaseAuth _auth = FirebaseAuth.instance;
  final FirebaseFirestore _firestore = FirebaseFirestore.instance;
  File? imageFile;
  Future getImage() async {
    ImagePicker _picker = ImagePicker();

    await _picker.pickImage(source: ImageSource.gallery).then((xFile) {
      if (xFile != null) {
        imageFile = File(xFile.path);
        uploadImage();
      }
    });
  }

  Future uploadImage() async {
    String fileName = Uuid().v1();
    int status = 1;

    await _firestore
        .collection('chatdetail')
        .doc(friendUid)
        .collection('chats')
        .doc(fileName)
        .set({
      "sendby": _auth.currentUser!.displayName,
      "message": "",
      "type": "img",
      "time": FieldValue.serverTimestamp(),
    });

    var ref =
    FirebaseStorage.instance.ref().child('images').child("$fileName.jpg");

    //error in ImageFile

    var uploadTask = await ref.putFile(imageFile!).catchError((error) async {
      await _firestore
          .collection('chatdetail')
          .doc(friendUid)
          .collection('chats')
          .doc(fileName)
          .delete();

      status = 0;
    });

    if (status == 1) {
      String imageUrl = await uploadTask.ref.getDownloadURL();

      await _firestore
          .collection('chatdetail')
          .doc(friendUid)
          .collection('chats')
          .doc(fileName)
          .update({"message": imageUrl});

      print(imageUrl);
    }
  }

CodePudding user response:

Replace import 'dart:html'; with 'import 'dart:io';'. You just imported the wrong package. File you want is from dart:io not dart:html.

  • Related