【问题标题】:send (post) data with image to RestFull API using Flutter使用 Flutter 将带有图像的数据发送(发布)到 RestFul API
【发布时间】:2020-10-28 01:49:42
【问题描述】:

我如何在颤振中将带有图像 url 的数据发送到 RestFull API,这是我的代码,但它不起作用,问题只是因为图像和所有数据发送成功:

 String imgUrl;

  insertService() async {
    var res = await http.post(
        "http://192.168.43.106:3000/service",
        headers: {},
        body: {
          "name" : namec.text,
          "file" : imgUrl,
          "desc" : descc.text,
          "price" : pricec.text,
          "cat" : widget.cid
        }
    );

    final data = json.decode(res.body);

    if(data['message'] != null && data['message'] == 'success'){
      Navigator.pushReplacement(context,
          MaterialPageRoute(builder: (context)=>Services(cid: widget.cid,)));
    }else{
      print("there is a problem in post request....");
    }
  }
@override
  void initState() {
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    final img = Padding(
      padding: EdgeInsets.all(20),
      child: InkWell(
        onTap: ()async{
          var imageIns =  await ImagePicker().getImage(source: ImageSource.gallery);
          setState(() {
            image = File(imageIns.path);
            imgUrl = imageIns.path;
          });

使用邮递员,即使使用图像也一切正常:

【问题讨论】:

    标签: api http flutter post


    【解决方案1】:

    imageIns.path 的确切类型是什么?如果它是一个对象,您可能需要将其编码为 json。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-25
      • 1970-01-01
      • 2020-11-08
      • 2023-01-16
      • 2021-12-16
      • 2019-03-09
      相关资源
      最近更新 更多