In Flutter How to get image path after selecting images using Multi Image Picker?

后端 未结 4 1437
执笔经年
执笔经年 2021-01-18 18:13

I want to get an image path from selected multiple images, I\'m using this link to select multiple images but I got assets, I want paths from selected multiple images becaus

4条回答
  •  耶瑟儿~
    2021-01-18 18:55

    I'm Using below Code to select multiple images by using file_picker: ^2.0.7 Library. Long press to select multiple image. Once Image Selected you can use f arr to display the images.

    List f = List();
    
     RaisedButton(
                child: Text("Pick Image"),
                onPressed: () async {
                  FilePickerResult result = await FilePicker.platform.pickFiles(
                    allowMultiple: true,
                    type: FileType.custom,
                    allowedExtensions: ['jpg', 'png', 'jpeg'],
                  );
                  if (result != null) {
                    f = result.paths.map((path) => File(path)).toList();
                    setState(() {});
                    print(f);
                  }
                },
              ),

    Sample API Call For image upload and normal data. Image uploaded column should be arr ( photo[] ).

     List newList = new List();
    
    
    Future ImageUpload() async {
    
    var request = http.MultipartRequest('POST', url);
     request.headers["Authorization"] = pref.getString("token");
        request.headers["Accept"] = "application/json";
       //Image Data
      for (int i = 0; i < f.length; i++) {
          newList.add(await http.MultipartFile.fromPath('photo[]', f[i].path));
        }
        request.files.addAll(newList);
        
        Map data = Map();
        //normal data
        data["user_id"] = user_id;
        data["project_id"] = pro_id;
        
        request.fields.addAll(data);
        var res = await request.send();
        
        if (res.statusCode == 200) {
          debugPrint("Status${res}");
          }else {
          debugPrint("status code${res}");
          }
          
        }
        

    Try This You can select and upload multiple images easily. Thank you.
    

提交回复
热议问题