我正在尝试更新博客文章的标题,内容和图像。我正在使用Dio进行API请求。
问题是,如果我尝试仅更新文本字段(例如标题),而内容文本则无法完美运行。但是,如果我尝试同时更新标题,内容和图像,那么将忽略标题或内容文本。
发出api请求以Dio和提供者的程序包更新带有图像文件的对象的实际方法是什么?
我的代码示例为:
表单代码:
import tkinter as tk
from tkinter import font as tkFont
root = tk.Tk()
helv35 = tkFont.Font(family='Helvetica', size=36)
optionsList = 'eggs spam toast'.split()
selectedOption = tk.StringVar(root, value=optionsList[0])
chooseTest = tk.OptionMenu(root, selectedOption, *optionsList)
chooseTest.config(font=helv35) # set the button font
helv20 = tkFont.Font(family='Helvetica', size=20)
menu = root.nametowidget(chooseTest.menuname)
menu.config(font=helv20) # Set the dropdown menu's font
chooseTest.grid(row=0, column=0, sticky='nsew')
root.mainloop()
提供商代码:
class _BlogPostCreateViewState extends State<BlogPostCreateView> {
bool _isUpdating = false;
BlogPost _blogPost = BlogPost();
String _imageUrl;
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
TextEditingController _titleCtrl = TextEditingController();
TextEditingController _contentCtrl = TextEditingController();
final picker = ImagePicker();
File _image;
void _getImage() async {
final pickedFile = await picker.getImage(source: ImageSource.gallery);
setState(() {
_image = File(pickedFile.path);
});
}
@override
Widget build(BuildContext context) {
ScreenArguments _arguments = ModalRoute.of(context).settings.arguments;
if (_arguments != null) {
_blogPost = Provider.of<ForumProvider>(context).getBlogPostDetail;
_titleCtrl.text = _blogPost.title;
_contentCtrl.text = _blogPost.content;
_imageUrl = _blogPost.contentImage;
_isUpdating = true;
}
return Scaffold(
appBar: AppBar(
elevation: 10,
title: Text('Share Thought'),
centerTitle: true,
),
body: SingleChildScrollView(
child: Container(
margin: EdgeInsets.symmetric(horizontal: 10.0, vertical: 50.0),
child: Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
TextFormField(
controller: _titleCtrl,
autofocus: true,
decoration: InputDecoration(
labelText: '* Blog Title',
),
validator: (value) {
if (value.isEmpty) {
return 'Blog Title is Required';
} else if (value.length > 100) {
return 'Title must be within 100 character';
}
},
onSaved: (value) {
_blogPost.title = value;
},
),
SizedBox(height: 20.0),
TextFormField(
controller: _contentCtrl,
keyboardType: TextInputType.multiline,
maxLines: null,
decoration: InputDecoration(
hintText: '(Post)',
),
validator: (value) {
if (value.length > 600) {
return 'Content must be within 600 characters';
}
if (_blogPost.content == null && _image == null) {
return 'Blog content or Image is Required';
}
},
onSaved: (value) {
_blogPost.content = value;
},
),
Column(
children: [
SizedBox(height: 10.0),
_image == null
? RaisedButton.icon(
onPressed: _getImage,
icon: Icon(
Icons.image,
color: Colors.white,
),
label: Text(
_imageUrl == null
? 'Attach Image'
: 'Change Image',
style: xAppBardTitle.copyWith(
color: Colors.white,
fontSize: 18.0,
),
),
color: Colors.teal.shade600,
)
: RaisedButton.icon(
onPressed: () {
setState(() {
_image = null;
});
},
icon: Icon(
Icons.cancel,
color: Colors.white,
),
label: Text(
_imageUrl == null
? 'Remove Image'
: 'Keep Previous',
style: xAppBardTitle.copyWith(
color: Colors.white,
fontSize: 18.0,
),
),
color: Colors.orange.shade500,
),
SizedBox(height: 10.0),
_isUpdating == false
? _image == null
? Container(
alignment: Alignment.center,
child: Icon(Icons.image,
size: 100, color: Colors.grey.shade300),
)
: Center(
child: Image(
image: FileImage(_image),
),
)
: _image == null && _imageUrl == null
? Container(
alignment: Alignment.center,
child: Icon(Icons.image,
size: 100, color: Colors.grey.shade300),
)
: _image == null
? Center(
child: Image(
image:
NetworkImage(_blogPost.contentImage),
),
)
: Center(
child: Image(
image: FileImage(_image),
),
),
],
),
SizedBox(height: 50.0),
Consumer<ForumProvider>(
builder: (_, providerFunction, __) {
return ReUseableRoundedButton(
buttonText: _isUpdating == true ? 'Update' : 'Submit',
onPressed: () async {
bool result;
if (!_formKey.currentState.validate()) {
return;
}
_formKey.currentState.save();
if (_isUpdating == false) {
result = await providerFunction.createABlogPost(
_blogPost,
_image,
);
} else {
result = await providerFunction.updateBlogPost(
_blogPost,
_image,
);
}
if (result == true) {
setState(() {
_image = null;
});
}
Fluttertoast.showToast(
msg: result == true ? 'Success' : 'Failed',
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 2,
backgroundColor: result == true
? Colors.green.shade700
: Colors.red.shade700,
textColor: Colors.white,
fontSize: 16.0,
);
providerFunction.fetchAllBlogs();
Navigator.pop(
context,
BlogListView.viewName,
);
},
);
},
)
],
),
),
),
),
);
}
}
API请求:
Future<bool> updateBlogPost(BlogPost blogPost, File image) async {
try {
var response = await ForumApiService().updateBlogPost(
blogPost: blogPost,
image: image,
);
var post = BlogPost.fromJson(response.data);
_setBlogDetail(post);
_blogListUpdate(post);
return true;
} catch (e) {
_setMessage('Could not Update Blog post because of $e');
print(e);
return false;
}
}