如何在 Flutter 中 return 一个 dio 输出
How to return a dio output in Flutter
我正在尝试做的是我正在尝试捕捉 dio 输出。
现在我正在尝试创建一篇具有 flutter future 功能的文章。
这是我的代码:
颤振
Future<dynamic> futureArticle;
String articleid;
futureArticle = CreateArticle(user.id, caption.text)
Dio Post 函数
CreateArticleImage(String author,String caption,) async {
try {
FormData formData = new FormData.fromMap({
'author' : author.toString(),
'caption' : caption,
});
Response response = await Dio().post("$SERVER_IP/api/articlecreate/", data: formData);
print(response.toString());
} catch (e) {
print(e);
}
}
Json输出
{
"id": "6ce0f013-d1fe-4f9f-bb72-0f1c8d21f64f",
"caption": "Caption",
},
我想做的是我想捕获 id 并 return 它像 articleid 一样飘动。
有人知道怎么做吗?
当消费一个 API 时,建议根据我们期望接收的数据创建一个 Class
:
class Article {
String id;
String caption;
Article({
this.id,
this.caption,
});
}
由于我们使用的是 API returns JSON 对象,我们可以实现 toJson
函数和 fromJson
工厂:
class Article {
String id;
String caption;
Article({
this.id,
this.caption,
});
factory Article.fromJson(Map<String, dynamic> json) => new Article(
id: json["id"] == null ? null : json["id"],
caption: json["caption"] == null ? null : json["caption"],
);
Map<String, dynamic> toJson() => {
"id": id == null ? null : id,
"caption": caption == null ? null : caption,
};
}
通过这样做,我们可以从 HTTP 响应创建一个 Article
class:
FormData formData = new FormData.fromMap({
'author' : author.toString(),
'caption' : caption,
});
Response response = await Dio().post("$SERVER_IP/api/articlecreate/", data: formData);
print(response.toString());
final jsonData = json.decode(response.body);
Article article = Article.fromJson(Map<String, String>.from(jsonData));
以下代码段包含了一个基于您指定场景的完整示例:
import 'dart:convert';
void getArticle(var user, var caption) async {
Future<dynamic> futureArticle;
String articleid;
futureArticle = await createArticle(user.id, caption.text);
print(futureArticle.id);
print(futureArticle.caption);
articleid = futureArticle.id;
}
Future<Article> createArticleImage(String author,String caption,) async {
try {
FormData formData = new FormData.fromMap({
'author' : author.toString(),
'caption' : caption,
});
Response response = await Dio().post("$SERVER_IP/api/articlecreate/", data: formData);
print(response.toString());
final jsonData = json.decode(response.body);
Article article = Article.fromJson(Map<String, String>.from(jsonData));
return article;
} catch (e) {
print(e);
}
}
class Article {
String id;
String caption;
Article({
this.id,
this.caption,
});
factory Article.fromJson(Map<String, dynamic> json) => new Article(
id: json["id"] == null ? null : json["id"],
caption: json["caption"] == null ? null : json["caption"],
);
Map<String, dynamic> toJson() => {
"id": id == null ? null : id,
"caption": caption == null ? null : caption,
};
}
我正在尝试做的是我正在尝试捕捉 dio 输出。 现在我正在尝试创建一篇具有 flutter future 功能的文章。 这是我的代码:
颤振
Future<dynamic> futureArticle;
String articleid;
futureArticle = CreateArticle(user.id, caption.text)
Dio Post 函数
CreateArticleImage(String author,String caption,) async {
try {
FormData formData = new FormData.fromMap({
'author' : author.toString(),
'caption' : caption,
});
Response response = await Dio().post("$SERVER_IP/api/articlecreate/", data: formData);
print(response.toString());
} catch (e) {
print(e);
}
}
Json输出
{
"id": "6ce0f013-d1fe-4f9f-bb72-0f1c8d21f64f",
"caption": "Caption",
},
我想做的是我想捕获 id 并 return 它像 articleid 一样飘动。 有人知道怎么做吗?
当消费一个 API 时,建议根据我们期望接收的数据创建一个 Class
:
class Article {
String id;
String caption;
Article({
this.id,
this.caption,
});
}
由于我们使用的是 API returns JSON 对象,我们可以实现 toJson
函数和 fromJson
工厂:
class Article {
String id;
String caption;
Article({
this.id,
this.caption,
});
factory Article.fromJson(Map<String, dynamic> json) => new Article(
id: json["id"] == null ? null : json["id"],
caption: json["caption"] == null ? null : json["caption"],
);
Map<String, dynamic> toJson() => {
"id": id == null ? null : id,
"caption": caption == null ? null : caption,
};
}
通过这样做,我们可以从 HTTP 响应创建一个 Article
class:
FormData formData = new FormData.fromMap({
'author' : author.toString(),
'caption' : caption,
});
Response response = await Dio().post("$SERVER_IP/api/articlecreate/", data: formData);
print(response.toString());
final jsonData = json.decode(response.body);
Article article = Article.fromJson(Map<String, String>.from(jsonData));
以下代码段包含了一个基于您指定场景的完整示例:
import 'dart:convert';
void getArticle(var user, var caption) async {
Future<dynamic> futureArticle;
String articleid;
futureArticle = await createArticle(user.id, caption.text);
print(futureArticle.id);
print(futureArticle.caption);
articleid = futureArticle.id;
}
Future<Article> createArticleImage(String author,String caption,) async {
try {
FormData formData = new FormData.fromMap({
'author' : author.toString(),
'caption' : caption,
});
Response response = await Dio().post("$SERVER_IP/api/articlecreate/", data: formData);
print(response.toString());
final jsonData = json.decode(response.body);
Article article = Article.fromJson(Map<String, String>.from(jsonData));
return article;
} catch (e) {
print(e);
}
}
class Article {
String id;
String caption;
Article({
this.id,
this.caption,
});
factory Article.fromJson(Map<String, dynamic> json) => new Article(
id: json["id"] == null ? null : json["id"],
caption: json["caption"] == null ? null : json["caption"],
);
Map<String, dynamic> toJson() => {
"id": id == null ? null : id,
"caption": caption == null ? null : caption,
};
}