如何解决使用ImagePicker上传图像
我正在开发Flutter项目,因此我尝试遵循有关Image Picker的一些教程,但是它不起作用。我不知道为什么。我的图像选择器可以工作,我可以选择图像,但是我的帖子请求不起作用。有人看错了吗?感谢您的阅读
class _HomeWidgetState extends State<HomeWidget> {
File imageFile;
final picker = ImagePicker();
_gallery() async {
final pickedFile = await ImagePicker.pickImage(source: ImageSource.gallery);
this.setState(() {
if (pickedFile != null) {
imageFile = File(pickedFile.path);
}
else {
print('no image');
}
});
String name = 'Demo Title';
String desc = 'Ma super description :)';
List<int> imageBytes = imageFile.readAsBytesSync();
String image = base64Encode(imageBytes);
String url = 'https://api.imgur.com/3/upload';
Map<String,String> headers = {"Authorization": "Bearer " + globals.access_token};
String json = '{"title": "$name","name": "$name","description": "$desc","image": "$image","type": "base64"}';
final response = await http.post(url,headers: headers,body: json);
if (response.statusCode != 200) {
return null;
}
print("********end************");
}
解决方法
您会尝试使用它吗?
class _HomeWidgetState extends State<HomeWidget> {
File imageFile;
final picker = ImagePicker();
_gallery() async {
final pickedFile = await ImagePicker.pickImage(source: ImageSource.gallery);
this.setState(() {
if (pickedFile != null) {
imageFile = File(pickedFile.path);
}
else {
print('no image');
}
});
String name = 'Demo Title';
String desc = 'Ma super description :)';
List<int> imageBytes = imageFile.readAsBytesSync();
String image = base64Encode(imageBytes);
String url = 'https://api.imgur.com/3/upload';
Map<String,String> headers = {"Authorization": "Bearer " + globals.access_token};
Map<String,dynamic> json = {"title": name,"name": name,"description": desc,"image": image,"type": "base64"};
final response = await http.post(url,headers: headers,body: json);
if (response.statusCode != 200) {
return null;
}
print("********end************");
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。