我有如下几个 IF 条件。
if (globals.chatroomInfoMap.containsKey(chatroomID)) {
if (messageData['name'] != null) {
globals.chatroomInfoMap[chatroomID].name =
messageData['name'];
print('object ${globals.chatroomInfoMap[chatroomID].name}');
}
print('1');
if (messageData['timestamp'] != null) {
globals.chatroomInfoMap[chatroomID].timestamp =
messageData['timestamp'];
print('object ${globals.chatroomInfoMap[chatroomID].timestamp}');
}
print('2');
if (messageData['members'] != null) {
globals.chatroomInfoMap[chatroomID].members =
messageData['members'];
print('object ${globals.chatroomInfoMap[chatroomID].members}');
}
print('3');
if (messageData['slogan'] != null) {
globals.chatroomInfoMap[chatroomID].slogan =
messageData['slogan'];
print('object ${globals.chatroomInfoMap[chatroomID].slogan}');
}
print('object here');
}
在输出中,我只能看到以下内容
I/flutter (25612): object OtherRoom!!@
I/flutter (25612): 1
所以我们看到的是第二个 IF 语句中打印的打印,下一行是之后打印的“1”。然后就是调试打印。我想知道这是什么魔法?我在 Dartpad 中尝试了同样的事情,以确保我没有正确使用语法。
Dartpad 中使用的代码:
main() {
final Map<String, dynamic> messageData = {
'slogan': 'buhuhu1',
'chatroomID': 'H8j0EHhu2QpicgFDGXYZ',
'mode': 'chatroom - update',
'name': 'OtherRoom@!',
'timestamp': '2019-02-23T17:12:12.000Z',
'click_action': 'FLUTTER_NOTIFICATION_CLICK',
'members': ["fb7TfdFvl7tWgtSldgHs", "8kG2kE4jBkPs2qwt7GYA"]
};
final Map<String, dynamic> global = {'H8j0EHhu2QpicgFDGXYZ': 'something'};
if (global.containsKey(messageData['chatroomID'])) {
if (messageData['name'] != null) {
print('here 1');
}
print('here');
if (messageData['timestamp'] != null) {
print('here 2');
}
}
}
输出:
here 1
here
here 2
你能帮我理解我错过了什么吗?我确信这是一些非常愚蠢的错误。
最佳答案
我遇到了问题。时间戳是不同的数据类型。我想知道为什么它没有抛出错误。感谢您的帮助。
关于dart - 嵌套 IF 条件出现意外行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54972958/
我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib
这道题是thisquestion的逆题.给定一个散列,每个键都有一个数组,例如{[:a,:b,:c]=>1,[:a,:b,:d]=>2,[:a,:e]=>3,[:f]=>4,}将其转换为嵌套哈希的最佳方法是什么{:a=>{:b=>{:c=>1,:d=>2},:e=>3,},:f=>4,} 最佳答案 这是一个迭代的解决方案,递归的解决方案留给读者作为练习:defconvert(h={})ret={}h.eachdo|k,v|node=retk[0..-2].each{|x|node[x]||={};node=node[x]}node[
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
我有一个用户工厂。我希望默认情况下确认用户。但是鉴于unconfirmed特征,我不希望它们被确认。虽然我有一个基于实现细节而不是抽象的工作实现,但我想知道如何正确地做到这一点。factory:userdoafter(:create)do|user,evaluator|#unwantedimplementationdetailshereunlessFactoryGirl.factories[:user].defined_traits.map(&:name).include?(:unconfirmed)user.confirm!endendtrait:unconfirmeddoenden
我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin
我有一个只接受一个参数的方法:defmy_method(number)end如果使用number调用方法,我该如何引发错误??通常,我如何定义方法参数的条件?比如我想在调用的时候报错:my_method(1) 最佳答案 您可以添加guard在函数的开头,如果参数无效则引发异常。例如:defmy_method(number)failArgumentError,"Inputshouldbegreaterthanorequalto2"ifnumbereputse.messageend#=>Inputshouldbegreaterthano
下面例子中的Nested和Child有什么区别?是否只是同一事物的不同语法?classParentclassNested...endendclassChild 最佳答案 不,它们是不同的。嵌套:Computer之外的“Processor”类只能作为Computer::Processor访问。嵌套为内部类(namespace)提供上下文。对于ruby解释器Computer和Computer::Processor只是两个独立的类。classComputerclassProcessor#Tocreateanobjectforthisc
我的假设是moduleAmoduleBendend和moduleA::Bend是一样的。我能够从thisblog找到解决方案,thisSOthread和andthisSOthread.为什么以及什么时候应该更喜欢紧凑语法A::B而不是另一个,因为它显然有一个缺点?我有一种直觉,它可能与性能有关,因为在更多命名空间中查找常量需要更多计算。但是我无法通过对普通类进行基准测试来验证这一点。 最佳答案 这两种写作方法经常被混淆。首先要说的是,据我所知,没有可衡量的性能差异。(在下面的书面示例中不断查找)最明显的区别,可能也是最著名的,是你的
我正在检查一个Rails项目。在ERubyHTML模板页面上,我看到了这样几行:我不明白为什么不这样写:在这种情况下,||=和ifnil?有什么区别? 最佳答案 在这种特殊情况下没有区别,但可能是出于习惯。每当我看到nil?被使用时,它几乎总是使用不当。在Ruby中,很少有东西在逻辑上是假的,只有文字false和nil是。这意味着像if(!x.nil?)这样的代码几乎总是更好地表示为if(x)除非期望x可能是文字false。我会将其切换为||=false,因为它具有相同的结果,但这在很大程度上取决于偏好。唯一的缺点是赋值会在每次运行
假设我在Ruby中有这个each循环。@list.each{|i|putsiifi>10breakend}我想循环遍历列表直到满足条件。这让我感到“不像Ruby”,因为我是Ruby的新手,是否有Ruby方法可以做到这一点? 最佳答案 您可以使用Enumerable#detect或Enumerable#take_while,取决于您想要的结果。@list.detect{|i|putsii>10}#Returnsthefirstelementgreaterthan10,ornil.正如其他人所指出的,更好的风格是先进行子选择,然后再对其