如何解决如何知道何时仍在 Stream builder 中加载 post 方法
所以我有一个表单,可以将数据发送到服务器,并且发送时我想显示循环加载。我在这里只显示接收数据时的加载,而不是发送时的加载。发送数据后,我想返回上一个屏幕。
界面 这里只显示数据发送到服务器时循环加载。
Widget build(BuildContext context) {
return StreamBuilder(
stream: manager.addPost,builder: (context,AsyncSnapshot<Post> snapshot) {
debugPrint(snapshot.connectionState.toString());
if (!snapshot.hasData) {
return buildColumn();
}else if (snapshot.hasError) {
return Center(
child: Text(
snapshot.error.toString(),style: TextStyle(fontSize: 20),),);
}
return Center(child: CircularProgressIndicator());
}
);
}
Widget buildColumn() =>
Form(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),child: buildTitle(),SizedBox(
height: 10,Padding(
padding: const EdgeInsets.all(8.0),child: buildDesription(),buildSubmit()
],));
Widget buildSubmit() => StreamBuilder<Object>(
stream: manager.isFormValid,snapshot) {
return ElevatedButton(onPressed: (){
if(snapshot.hasData){
manager.submit();
debugPrint("YEs");
}
},child: Text("SEND"));
});
提交
Future submit() async {
String title = _title.value;
String description = _description.value;
Post itemModel = await _repository.addPost(title,description);
_postsFetcher.sink.add(itemModel);
return itemModel;
}
回购
final postRepo = CreatePostRepository();
Future<Post> addPost(String title,String description) => postRepo.createPost(title,description);
最后是post方法
class CreatePostRepository {
Future<Post> createPost(String title,String description) async {
final response = await http.post(
Uri.parse(getPostUrl),headers: <String,String>{
'Content-Type': 'application/json; charset=UTF-8',},body: jsonEncode(
<String,dynamic>{"title": title,"body": description,"userId": 1}),);
if (response.statusCode == 201) {
print(' created 201: ');
print(response.body.toString());
return Post.fromJson(jsonDecode(response.body));
} else {
print('not throw: ');
throw Exception('failed to add post');
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。