【问题标题】:Creating API and REST with image picker (Flutter)使用图像选择器 (Flutter) 创建 API 和 REST
【发布时间】:2021-08-01 17:07:48
【问题描述】:

您好,当您使用图像选择器从图库中选择图像时,我想制作它会通过 REST 将其发送到 API。我正在寻找如何做到这一点,但我仍然不明白,我尝试了不同的方法,但总是发生数百万个错误。因此,如果有人知道我将图像作为多部分数据发送到 API 的最佳(最简单)方法是什么,那将意味着全世界。我的目标是将flutters前端与pythons后端连接起来。

到目前为止,这是我的代码:

class _MyHomePageState extends State<MyHomePage> {
  Future <File> file;

  void openGatePhoto() async {
    // ignore: deprecated_member_use
  this.setState(() {
     file =  ImagePicker.pickImage(source: ImageSource.gallery);
    });



    showDialog(
        context: context,
        builder: (context) => CustomDialogOpen(
              title: "Gates are open",
              description:
                  "Licence plate numbers have been successfully stored in the database",
            ));
  }

此代码的作用是从图库中拍照,然后在您选择它后,它会显示此弹出消息。在此先感谢:D

【问题讨论】:

    标签: image api flutter rest dart


    【解决方案1】:

    首先让我们选择一张图片

    ` finalpickFile = await picker.getImage(source: ImageSource.camera);

    then ifpickedFile !=null`我们可以上传到你的rest api然后如果成功我们可以显示对话框

    你可以使用类似flutter bloc的东西来更好地管理这个

    `
    Response response;
    var dio = Dio();
    getAndUploadImage(){
    final pickedFile = await picker.getImage(source: ImageSource.camera);
      if (pickedFile != null) {
    //upload to your api
    //i'll use [dio][1] here 
    var file=File(pickedFile.path);
    var formData = FormData.fromMap({
      'name': 'Jonh ',
      "myfile": await MultipartFile.fromFile(
              file.path,
              filename: file.path,
            ),
    
         });
    var response = await dio.post('myUrl', data: formData);
    if(response.statusCode==200){
    showDialog(
            context: context,
            builder: (context) => CustomDialogOpen(
                  title: "Gates are open",
                  description:
                      "Licence plate numbers have been successfully stored in the database",
                ));
    }
    }else{
    ..
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-29
      • 1970-01-01
      • 2021-02-11
      • 2021-02-05
      • 1970-01-01
      • 2020-07-10
      • 1970-01-01
      • 2020-10-08
      相关资源
      最近更新 更多