Json Dart Http客户端返回截断响应,当我打印到控制台时,截断略有不同

Json Dart Http客户端返回截断响应,当我打印到控制台时,截断略有不同,json,http,dart,flutter,Json,Http,Dart,Flutter,我正在尝试读取JSON响应并使用Flatter的FutureBuilder构建UI,但无法在客户端上获得整个JSON响应。当我试图用两种不同的打印语句打印响应时,它们之间的内容略有不同。请注意下面的控制台打印输出对于My Posts API响应和My Posts Json响应字符串是如何不同的 有人能解释一下这种行为,以及在客户端收到完整的JSON数组之前如何实现正确的侦听吗 预期的API响应- [{"_id":"5bd11c9b8a9fc0a744d1bebc","postId":1,"pos

我正在尝试读取JSON响应并使用Flatter的FutureBuilder构建UI,但无法在客户端上获得整个JSON响应。当我试图用两种不同的打印语句打印响应时,它们之间的内容略有不同。请注意下面的控制台打印输出对于My Posts API响应My Posts 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');
            }
            }
        });
    }
}
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

这只是控制台在这么多字符后截断

请注意,您正在打印两种不同的内容:

getMyPostsApiResponse.body
是从服务器接收的json


json.decode(getMyPostsApiResponse.body).toString()
是已解码的
列表的toString(不包括引号,这就是字符串长度不同的原因)

这只是控制台在这么多字符后截断的结果

请注意,您正在打印两种不同的内容:

getMyPostsApiResponse.body
是从服务器接收的json


json.decode(getMyPostsApiResponse.body).toString()
是已解码的
列表的toString(不包括引号,这就是字符串长度不同的原因)

看起来您正在安卓上尝试打印长字符串。引自:

函数的作用是:输出到系统控制台,您可以 使用颤振日志(基本上是adb的包装器)查看 logcat)

如果一次输出太多,Android有时会丢弃一些 日志行。为了避免这种情况,您可以使用来自Flatter的debugPrint() 基金会图书馆。这是一个围绕打印的包装器,它会限制打印速度 输出到避免被Android内核丢弃的级别

因此,您可以尝试改用
debugPrint


导入flatter包以使用debugPrint-
Import'Package:flatter/foundation.dart'

看起来您正在Android上尝试打印长字符串。引自:

函数的作用是:输出到系统控制台,您可以 使用颤振日志(基本上是adb的包装器)查看 logcat)

如果一次输出太多,Android有时会丢弃一些 日志行。为了避免这种情况,您可以使用来自Flatter的debugPrint() 基金会图书馆。这是一个围绕打印的包装器,它会限制打印速度 输出到避免被Android内核丢弃的级别

因此,您可以尝试改用
debugPrint


导入flatter包以使用debugPrint-
Import'Package:flatter/foundation.dart'

由于缺少引号,因此由于JSON解码,响应的长度略有变化,但这并不能解释响应的截断。无论如何,谢谢:)您对缺少的引号有一点看法,因此由于JSON解码,响应的长度略有变化,但这并不能解释响应的截断。无论如何谢谢:)谢谢@ringil,我从文档中学到了一些关于debugPrint和flatter中的一般调试的知识,但是当我使用
debugPrint
debugPrint同步方法打印时,截断总是存在的,但是谢谢!)请注意,如果
jsonDecode
成功,则不太可能在该点之前被截断,否则将导致解码错误。截断仍然可能发生在打印过程中的某个地方。谢谢@ringil,我从文档中了解了关于debugPrint和flifter中的一般调试的一些内容,但是当我使用
debugPrint
debugPrint同步打印时,截断总是存在的,但是谢谢!)请注意,如果
jsonDecode
成功,则不太可能在该点之前被截断,否则将导致解码错误。截短仍有可能发生在印刷过程中的某个地方。