首页 > 解决方案 > 使用 Flutter 将带有图像的数据发送(发布)到 RestFull API

问题描述

我如何在颤动中将带有图像 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;
          });

和邮递员一起,即使有图像,一切都很好: 在此处输入图像描述

标签: apihttpflutterpost

解决方案


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


推荐阅读