You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
换成这样
this_properties_value = ''
if t == 'int':
this_properties_value = '0'
if t == 'bool':
this_properties_value = 'false'
if t == 'Object':
this_properties_value = 'null'
if t == 'String':
this_properties_value = "''"
Activity
TshineZheng commentedon Aug 16, 2018
+1
debuggerx01 commentedon Aug 16, 2018
@tom0gao @TshineZheng 给个会出错的json例子?没明白具体是哪种情况。
从描述猜测是不是出现了解析为List的字段,后台在没有值的时候不是传[]而是null了?
tom0gao commentedon Aug 16, 2018
@debuggerx01 对 就是没传这个字段 然后去解析 就 抛异常了
debuggerx01 commentedon Aug 16, 2018
@tom0gao 那我大概明白了,想想怎么做防御。
这种情况应该只会出现在List上,一般数据类型字段应该不会有问题,你最上面给的代码没必要吧?
TshineZheng commentedon Aug 16, 2018
@debuggerx01 对象嵌套的时候也应该加入空判断。
我尝试在
template_code.py模版中的 fromJson 后面加入了判断就好了。因为不是很会 py,没有仔细阅读源码,不确定这样会是否最优,所以没提交pr。
tom0gao commentedon Aug 16, 2018
@debuggerx01 对 一般只有 数组才出问题 @TshineZheng 你这个判断最上层可以在基类里面处理掉
// result解析抛出去
if (buildFun != null && jsonRes['result'] != null) {
data = buildFun(jsonRes['result']);
}
还有你return掉之后下面如果还有需要解析的也会停止 一般不要return
TshineZheng commentedon Aug 16, 2018
@tom0gao 不是很明白你说的基类是指什么。
当传进来的 jsonRes 本身就是空的情况下,后续就没必要解析了。
tom0gao commentedon Aug 16, 2018
@TshineZheng
参考作者的样例
class BaseResp {
int code;
String msg;
T data;
factory BaseResp(jsonStr, Function buildFun) =>
jsonStr is String ? BaseResp.fromJson(json.decode(jsonStr), buildFun) : BaseResp.fromJson(jsonStr, buildFun);
BaseResp.fromJson(jsonRes, Function buildFun) {
code = jsonRes['code'];
msg = jsonRes['msg'];
/// 这里可以做code和msg的处理逻辑
if (buildFun != null && jsonRes['data'] != null) {
data = buildFun(jsonRes['data']);
}
}
}
debuggerx01 commentedon Aug 16, 2018
@tom0gao @TshineZheng
我打算把数组解析部分替换成下面的形式:
感觉就没问题了,你们二位怎么看?
BaseResp那边的前置处理应该自己根据实际情况做各种处理,和我后面通用的解析代码其实没什么关系。生成的bean代码其实只有一点要求,就是能够忠实地表达传递过来json的实际信息。
tom0gao commentedon Aug 16, 2018
@debuggerx01
if (jsonRes['data'] == null) {
data = null; //这里替换成[]应该会好一点 防止外面调用for时空指针
} else {
data = [];
for (var dataItem in jsonRes['data']) {
data.add(new Data.fromJson(dataItem));
}
}
debuggerx01 commentedon Aug 16, 2018
@tom0gao 这就是我上面说的,bean代码需要能够忠实地表达传递过来json的实际信息。json里面字段的值如果就是null,那么我生成的对象就不能给默认值。。因为有些后台就是把[]和null当做数组数据的两种状态,需要前端分情况自己处理。
TshineZheng commentedon Aug 16, 2018
@tom0gao 原来是指范例的基类啊,我是在偶然情况下看到这个项目的,所以没见过这个。。。
@debuggerx01 不嫌麻烦的话, 要不加一个是否可以为空的开关?nullable?
debuggerx01 commentedon Aug 16, 2018
@TshineZheng 说实话还真有点麻烦……主要是我一开始就没想做这个功能,因为我之前的经验来看给字段设置nullable很多情况下会导致结果的不一致,只会给开发和后续的逻辑造成混乱,也就是说很有可能json解析过程中就“'隐藏了潜在的问题”,就比如上面提到的某些后台接口就是把[]和null当做数组数据的两种状态,这时候如果提供nullable的开关,开发必须非常清楚地认识后台接口的含义才能正确判断到底应不应该打开这个开关,结果来说就是很有可能带来难以察觉的bug。
debuggerx01 commentedon Aug 17, 2018
@tom0gao @TshineZheng 我更新了代码,如果方便的话clone一下最新代码跑跑看是不是解决了你们的问题?

我简单写的测试代码运行效果如下:
TshineZheng commentedon Aug 18, 2018
@debuggerx01
我这边试了下多层嵌套和List, 暂时没发现问题.
辛苦了, 谢谢.
debuggerx01 commentedon Aug 21, 2018
@tom0gao @TshineZheng 我发布了新的release包,方便的话请帮我测试下有没有问题,谢谢~~
tom0gao commentedon Aug 22, 2018
@debuggerx01 好的,多谢分享
20156284 commentedon Jan 3, 2019
很感谢您的回复
我给他赋初始值 主要是方便 做 页面 的时候 调用到
比如说 一个APP 用户的 个人页面 用户名称 我再写页面的时候 就可以直接用 UserModel 类中的 username给他填充 这样 回头网络请求下来 就不会 刷新下当前的页面 我就不用再做 判断 是否 UserModel 是不是空的 username 是不是 空的
如果像您 那样 没有赋个初始值 那么UserModel 下所有的 属性都是null
然后flutter 的Text 控件 不能使用null 给 他的text
故改了一下。
如果 有和我一样需求的 朋友 可以试试用这里
把的 tools.py 改了一下
换成这样
this_properties_value = ''
if t == 'int':
this_properties_value = '0'
if t == 'bool':
this_properties_value = 'false'
if t == 'Object':
this_properties_value = 'null'
if t == 'String':
this_properties_value = "''"
先按照基本数据类型方式处理
properties = ' %s %s;\n' % (t, n)
this_properties = '\nthis.%s=%s, ' % (n,this_properties_value)
construction = ' %s = jsonRes['%s'];\n' % (n, f)
to_string = '"%s": $%s,' % (f, n)
以后生成的 xxx.fromParams(); 具有重载的属性
debuggerx01 commentedon Jan 3, 2019
@20156284
原来是这个意思……感觉跟这个issue也不太一样。。
当然你选择这种方式作为数据请求成功前的placeholder也是可以的,不过其实可以试试flutter的StreamBuilder处理ui加载异步数据的问题,并建议使用进度条一类的方式给用户一个提示,而不是直接显示默认的空数据。