[英]I cant upload my image file to server (Flutter-API)
I made a form in my apps, but the image field didn't parse any image to server. 我在应用程序中创建了一个表单,但是图像字段没有将任何图像解析到服务器。 I use image picker from gallery.
我使用图库中的图片选择器。 This is my get image code from gallery:
这是我从图库获得的图像代码:
Future getImageFromGallery() async {
var image = await ImagePicker.pickImage(source: ImageSource.gallery);
setState(() {
_image = image;
});
}
and this is my body apps code with text field and image picker from gallery: 这是我的身体应用程序代码,带有来自画廊的文本字段和图像选择器:
Padding(
padding: const EdgeInsets.only(top: 8.0),
child: new TextFormField(
maxLines: 3,
decoration: new InputDecoration(
labelText: "Keterangan"),
onSaved: (String val) => description = val,
),
),
Padding(
padding: const EdgeInsets.all(15.0),
child: SizedBox(
width: 100.0,
height: 100.0,
child: RaisedButton(
child: _image == null
? Icon(Icons.add_a_photo)
: Image.file(_image),
onPressed: () {
this.getImageFromGallery();
},
),
),
),
Padding(
padding: const EdgeInsets.only(top: 40.0),
child: RaisedButton(
child: Text("Submit"),
color: Colors.blue,
onPressed: () {
newPost();
},
And this is my newPost method : 这是我的newPost方法:
void newPost() async {
Map data = {
"destination": destination,
"start": start,
"finish": finish,
"person": person,
"route": route,
"descrption": description,
"image": image
};
var body = json.encode(data);
String token = await getToken();
var response = await http.post('https://api-wisapedia.herokuapp.com/posts/',
headers: {HttpHeaders.authorizationHeader: 'Bearer $token'},
body: body);
_newPostResponse =
NewPostResponse.fromJson(json.decode(response.body.toString()));
if (token != null) {
if (destination.isEmpty ||
start.isEmpty ||
finish.isEmpty ||
person.isEmpty ||
route.isEmpty ||
description.isEmpty ||
image.isEmpty) {
showDialog(
builder: (context) => AlertDialog(
content: Text('Please fill the form'),
actions: <Widget>[
FlatButton(
onPressed: () {
Navigator.pop(context);
},
child: Text('OK'),
)
],
),
context: context);
} else {
_formkey.currentState.save();
Navigator.push(
context, MaterialPageRoute(builder: (context) => MainScreen()));
}
} else {
showDialog(
builder: (context) => AlertDialog(
content: Text('You need to authenticate'),
actions: <Widget>[
FlatButton(
onPressed: () {
Navigator.pop(context);
},
child: Text('$token'),
)
],
),
context: context);
}
}
There is onSaved
method in text field that makes values of this field can be uploaded by API, but there isn't onSaved
method for image. 文本字段中有
onSaved
方法,可以使该字段的值可以通过API上传,但是图像没有onSaved
方法。 I'm confused is : Image.file(_image)
have a same role as onSaved
or not? 我很困惑是
: Image.file(_image)
有一个相同的角色onSaved
与否? Then when I run this code, I don't know why but the image non uploaded. 然后,当我运行此代码时,我不知道为什么,但是图像未上传。 how can I solve this problem?
我怎么解决这个问题?
I think you should await your function, since it's returning a Future. 我认为您应该等待您的功能,因为它正在返回Future。 Something like this:
像这样:
onPressed: () async {
await this.getImageFromGallery();
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.