[英]How can I send from Flutter to a web service information using form-data (image, latitude, longitude, etc)?
我目前正在开发一个需要将图像和其他信息发送到 web 应用程序的移动应用程序。 我已经有了需要发送的信息的图片和地理位置。 但是,我无法以表单数据结构将该信息发送到我的 web 服务。
我需要发送此信息的 flutter 屏幕是这样构建的:
class ConfirmationScreen extends StatefulWidget {
final XFile? pictureFile;
final Uri uri = 'http://127.0.0.1:5000/analyze';
final Position? position;
const ConfirmationScreen({this.pictureFile, this.position, Key? key}) : super(key: key);
@override
State<ConfirmationScreen> createState() => _ConfirmationScreenState();
}
class _ConfirmationScreenState extends State<ConfirmationScreen> {
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text("Confirmation")),
body: Column(
children: [
Image.file(File(widget.pictureFile!.path)),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.red,
textStyle: const TextStyle(color: Colors.white)
),
onPressed: (){},
child: const Text("BACK"),),
const Padding(
padding: EdgeInsets.fromLTRB(30.0,0,0,0)),
ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.green,
textStyle: const TextStyle(color: Colors.white)
),
// Image.file(File(widget.pictureFile!.path))
onPressed: (){
// need to send the form-data information here
},
child: const Text("SEND"))
],
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
style: const TextStyle(
backgroundColor: Colors.white),
widget.position!.latitude.toString()
)
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
style: const TextStyle(
backgroundColor: Colors.white),
widget.position!.longitude.toString()
)
),
],
),
);
}
}
我在使用这种类型的图像以及如何使用 flutter 将更多图像发送到我的 web 服务时遇到了很多麻烦。 如果有人知道可以帮助我解决此问题的依赖项或 function,请在评论中告诉我。 谢谢!!
在http package 上使用MultipartRequest 。
var request = http.MultipartRequest('POST', Uri.https('example.com', 'create'))
..fields['user'] = 'nweiz@google.com'
..files.add(await http.MultipartFile.fromPath(
'package', 'build/package.tar.gz',
contentType: MediaType('application', 'x-tar')));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.