我正在尝试读取JSON响应并使用Flutter的FutureBuilder构建UI,但无法在客户端上获得整个JSON响应。当我尝试在两个不同的打印语句中打印响应时,它们之间的内容差别很小。请注意,以下控制台打印输出对于我的帖子API响应和我的帖子Json响应字符串。
有人可以解释这种行为以及如何在客户端接收到完整的JSON数组之前实施适当的侦听吗?
预期的API响应-
[{"_id":"5bd11c9b8a9fc0a744d1bebc","postId":1,"postUserId":1,"postDescription":"First Post with 2 Photos","postLongDescription":"First Post with 2 Photos","postDateTime":"2018-07-27T10:50:42.389Z","postLocationId":1,"postHasMedia":true,"postActive":true,"postLikesCount":3,"postCommentsCount":1},{"_id":"5bd11c9b8a9fc0a744d1bebd","postId":2,"postUserId":2,"postDescription":"Second Post with 1 Video","postLongDescription":"Second Post with 1 Video","postDateTime":"2018-07-27T11:02:00.389Z","postLocationId":2,"postHasMedia":true,"postActive":true,"postLikesCount":12,"postCommentsCount":2},{"_id":"5bd11c9b8a9fc0a744d1bebe","postId":3,"postUserId":2,"postDescription":"Third Post with No Video","postLongDescription":"Third Post with No Video","postDateTime":"2018-07-27T11:12:34.389Z","postLocationId":3,"postHasMedia":false,"postActive":true,"postLikesCount":9,"postCommentsCount":0},{"_id":"5bd11c9b8a9fc0a744d1bebf","postId":4,"postUserId":3,"postDescription":"Fourth Post with 1 Photo but Disabled","postLongDescription":"Fourth Post with 1 Photo but Disabled","postDateTime":"2018-07-27T11:12:34.389Z","postLocationId":2,"postHasMedia":true,"postActive":false,"postLikesCount":4,"postCommentsCount":0}]
颤振区码-
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:MyApp/models/post_model.dart';
import 'package:http/http.dart' as http;
enum storyTypes {
timeline,
myposts
}
class PostsBloc {
Future<PostModel> getPosts(storyTypes storyType) async {
if (storyType == storyTypes.myposts) {
final String url = "http://127.0.0.1:8081/posts/all";
return await http.get(url).then((getMyPostsApiResponse) {
if (getMyPostsApiResponse.statusCode != 200) {
throw Exception("Error with http over network");
}
else {
if (getMyPostsApiResponse.statusCode == 200) {
print('My Posts API Response - ' + getMyPostsApiResponse.body);
print('My Posts Json Response String - ' + json.decode(getMyPostsApiResponse.body).toString());
return PostModel.fromJson(json.decode(getMyPostsApiResponse.body));
}
else {
throw Exception('Failed to load post');
}
}
});
}
}
Flutter控制台输出-
I/flutter (32316): My Posts API Response - [{"_id":"5bd11c9b8a9fc0a744d1bebc","postId":1,"postUserId":1,"postDescription":"First Post with 2 Photos","postLongDescription":"First Post with 2 Photos","postDateTime":"2018-07-27T10:50:42.389Z","postLocationId":1,"postHasMedia":true,"postActive":true,"postLikesCount":3,"postCommentsCount":1},{"_id":"5bd11c9b8a9fc0a744d1bebd","postId":2,"postUserId":2,"postDescription":"Second Post with 1 Video","postLongDescription":"Second Post with 1 Video","postDateTime":"2018-07-27T11:02:00.389Z","postLocationId":2,"postHasMedia":true,"postActive":true,"postLikesCount":12,"postCommentsCount":2},{"_id":"5bd11c9b8a9fc0a744d1bebe","postId":3,"postUserId":2,"postDescription":"Third Post with No Video","postLongDescription":"Third Post with No Video","postDateTime":"2018-07-27T11:12:34.389Z","postLocationId":3,"postHasMedia":false,"postActive":true,"postLikesCount":9,"postCommentsCount":0},{"_id":"5bd11c9b8a9fc0a744d1bebf","postId":4,"postUserId":3,"postDescription":"Fourth Post with 1 Photo but Dis
I/flutter (32316): My Posts Json Response String - [{_id: 5bd11c9b8a9fc0a744d1bebc, postId: 1, postUserId: 1, postDescription: First Post with 2 Photos, postLongDescription: First Post with 2 Photos, postDateTime: 2018-07-27T10:50:42.389Z, postLocationId: 1, postHasMedia: true, postActive: true, postLikesCount: 3, postCommentsCount: 1}, {_id: 5bd11c9b8a9fc0a744d1bebd, postId: 2, postUserId: 2, postDescription: Second Post with 1 Video, postLongDescription: Second Post with 1 Video, postDateTime: 2018-07-27T11:02:00.389Z, postLocationId: 2, postHasMedia: true, postActive: true, postLikesCount: 12, postCommentsCount: 2}, {_id: 5bd11c9b8a9fc0a744d1bebe, postId: 3, postUserId: 2, postDescription: Third Post with No Video, postLongDescription: Third Post with No Video, postDateTime: 2018-07-27T11:12:34.389Z, postLocationId: 3, postHasMedia: false, postActive: true, postLikesCount: 9, postCommentsCount: 0}, {_id: 5bd11c9b8a9fc0a744d1bebf, postId: 4, postUserId: 3, postDescription: Fourth Post with 1 Photo but Disabled, postLongDescr
答案 0 :(得分:3)
好像您在Android上尝试打印长字符串。引用documentation about debugging:
Dart print()函数输出到系统控制台,您可以 使用颤动日志(基本上是adb的包装器)进行查看 logcat)。
如果一次输出太多,则Android有时会丢弃一些 日志行。为避免这种情况,您可以使用Flutter的 基础图书馆。这是围绕打印的包装,可限制打印 输出到避免被Android内核丢弃的水平。
所以您可以尝试使用debugPrint
。
导入Flutter软件包以使用debugPrint-import 'package:flutter/foundation.dart';
答案 1 :(得分:2)
那只是控制台在输入这么多字符后被截断。
请注意,您要打印两种不同的东西:
getMyPostsApiResponse.body
是从服务器接收的json。
json.decode(getMyPostsApiResponse.body).toString()
是解码后的List
的toString(不包括引号,这就是字符串长度不同的原因)