skip to Main Content

To have a better view this is the Image


  void _addImage(c) async {
    final ImagePicker picker = ImagePicker();
    final XFile? image = await picker.pickImage(source: ImageSource.gallery);
    print(image?.path);
    print(_imagePath?.split('/').last);
    if (image != null) {
      setState(() {
        String _imagePath = attachments.add(image.path);
      });
    }
  }
}

It was invalid, it was to render image uploaded name.

3

Answers


  1. Chosen as BEST ANSWER

    **

    This worked out

    **

    Future<void> _addImage() async {
        final ImagePicker picker = ImagePicker();
        final XFile? image = await picker.pickImage(source: ImageSource.gallery);
        print(image?.path);
        if (image != null) {
          setState(() {
            String _imagePath = image!.path;
            attachments.add(_imagePath);
            imageName = [_imagePath.split('/').last];
            print(_imagePath.split('/').last);
          });
        }
      }
    

  2. Your code should be like..

    Future<void> _addImage(c) async {
        final ImagePicker picker = ImagePicker();
        final XFile? image = await picker.pickImage(source: ImageSource.gallery);
        print(image?.path);
        print(_imagePath?.split('/').last);
        if (image != null) {
          setState(() {
            String _imagePath =  image!.path;
            attachments.add(_imagePath);
          });
        }
      }
    }
    
    Login or Signup to reply.
  3. You should write it like this:

    Future<void> _addImage(c) async {
        final ImagePicker picker = ImagePicker();
        final XFile? image = await picker.pickImage(source: ImageSource.gallery);
        print(image?.path);
        print(_imagePath?.split('/').last);
        if (image != null) {
          setState(() {
            attachments.add(image!.path); 
            //removed _imagePath variable because it was being used just to carry
            //the value of image!.path 
            //if you want to use it as a global variable please define it outside
            //the function scope
            // String imagePath = "";
            // _addImage(c){setState((){imagePath = image!.path;});}
          });
        }
      }
    }
    

    Couple of things I noticed in this function:

    1. c parameter is never used
    2. _imagePath?.split('/').last could break depending upon what OS you run it on
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search