Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Dart 发送带有特定要求的post请求颤振_Dart_Flutter_Httprequest - Fatal编程技术网

Dart 发送带有特定要求的post请求颤振

Dart 发送带有特定要求的post请求颤振,dart,flutter,httprequest,Dart,Flutter,Httprequest,我需要一些关于如何使用带有一些参数的flatter中的http模块发送POST请求的建议 我需要将用户名设置为字符串(在请求正文中),还需要将属性设置为正文中的文件。在颤振上执行请求的最简单方法是使用 如果您的json负载是 {"username":"johndoe", "image":"base64 image data"} 在dio中,代码看起来像 import "dart:io"; import "dart:convert"; import 'package:dio/dio.dart';

我需要一些关于如何使用带有一些参数的flatter中的
http
模块发送
POST
请求的建议


我需要将
用户名
设置为字符串(在请求正文中),还需要将属性设置为正文中的文件。

在颤振上执行请求的最简单方法是使用

如果您的json负载是

{"username":"johndoe", "image":"base64 image data"}
在dio中,代码看起来像

import "dart:io";
import "dart:convert";
import 'package:dio/dio.dart';

// read image bytes from disk as a list
List<int> imageBytes = File("./image.png").readAsBytesSync();

// convert that list to a string & encode the as base64 files
String imageString = base64Encode(imageBytes);

// Send a post request to server
dio.post("/url-to-post-to", data: {"username":"johndoe", "image":imageString});
导入“dart:io”;
导入“省道:转换”;
进口“包装:dio/dio.dart”;
//以列表形式从磁盘读取图像字节
List imageBytes=文件(“./image.png”).readAsBytesSync();
//将该列表转换为字符串并将其编码为base64文件
字符串imageString=base64Encode(imageBytes);
//向服务器发送post请求
post(“/url to post to”,数据:{“username”:“johndoe”,“image”:imageString});

此答案的可能副本可能会有所帮助-。特别是答案的base64字符串部分,然后只需使用dart:convert lib to json.encode映射即可。如果我正确理解了您的问题?当我设置注册端点的post请求时,我需要向参数添加一个文件。您的意思是上载一个文件吗?是的,上载一个文件我的friendHope在中回答了您的问题