Flutter 联调问题

碰到什么错?

在我们进行Flutter Native混合开发的过程中,vscode连真机/模拟器调试时总是会出现一些莫名其妙的错误。

例如:

image

<pre data-language="bash" id="FR5I9" class="ne-codeblock language-bash" style="border: 1px solid #e8e8e8; border-radius: 2px; background: #f9f9f9; padding: 16px; font-size: 13px; color: #595959">➜ flutter attach
There are multiple observatory ports available.
Rerun this command with one of the following passed in as the appId:

flutter attach --app-id com.xxx.xxx (2)
flutter attach --app-id com.xxx.xxx</pre>

导致attach失败,混合调试变的异常麻烦。

在网上查阅了各种资料,大致都是说网络有问题,可能是本地的设置开了代理或者让重启设备、重启应用等。

但是尝试了许多方法都不能解决问题。

在尝试的过程中,通过终端之前去调用attach时,发现了一些可能存在问题的地方。

<pre data-language="basic" id="RXvZf" class="ne-codeblock language-basic" style="border: 1px solid #e8e8e8; border-radius: 2px; background: #f9f9f9; padding: 16px; font-size: 13px; color: #595959">➜ flutter_business git:(feature/S_2108) flutter attach -v
[ +138 ms] executing: [/Users/dangkun/flutter_sdk/flutter/] git -c log.showSignature=false log -n 1 --pretty=format:%H
[ +54 ms] Exit code 0 from: git -c log.showSignature=false log -n 1 --pretty=format:%H
[ ] 1aafb3a8b9b0c36241c5f5b34ee914770f015818
[ ] executing: [/Users/dangkun/flutter_sdk/flutter/] git tag --points-at HEAD
[ +40 ms] Exit code 0 from: git tag --points-at HEAD
[ ] 1.22.4
[ +9 ms] executing: [/Users/dangkun/flutter_sdk/flutter/] git rev-parse --abbrev-ref --symbolic @{u}
[ +19 ms] Exit code 0 from: git rev-parse --abbrev-ref --symbolic @{u}
[ ] origin/stable
[ ] executing: [/Users/dangkun/flutter_sdk/flutter/] git ls-remote --get-url origin
[ +18 ms] Exit code 0 from: git ls-remote --get-url origin
[ ] https://github.com/flutter/flutter.git
[ +61 ms] Unable to locate an Android SDK.
[ +8 ms] executing: [/Users/dangkun/flutter_sdk/flutter/] git rev-parse --abbrev-ref HEAD
[ +46 ms] Exit code 0 from: git rev-parse --abbrev-ref HEAD
[ ] stable
[ +8 ms] executing: sw_vers -productName
[ +19 ms] Exit code 0 from: sw_vers -productName
[ ] macOS
[ ] executing: sw_vers -productVersion
[ +17 ms] Exit code 0 from: sw_vers -productVersion
[ ] 11.0.1
[ ] executing: sw_vers -buildVersion
[ +16 ms] Exit code 0 from: sw_vers -buildVersion
[ ] 20B29
[ +1 ms] executing: sysctl hw.optional.arm64
[ +5 ms] Exit code 1 from: sysctl hw.optional.arm64
[ ] sysctl: unknown oid 'hw.optional.arm64'
[ +52 ms] Artifact Instance of 'AndroidMavenArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'AndroidGenSnapshotArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'AndroidInternalBuildArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'IOSEngineArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'FlutterWebSdk' is not required, skipping update.
[ +4 ms] Artifact Instance of 'WindowsEngineArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'MacOSEngineArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'LinuxEngineArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'LinuxFuchsiaSDKArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'MacOSFuchsiaSDKArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'FlutterRunnerSDKArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'FlutterRunnerDebugSymbols' is not required, skipping update.
[ +10 ms] Artifact Instance of 'MaterialFonts' is not required, skipping update.
[ ] Artifact Instance of 'GradleWrapper' is not required, skipping update.
[ ] Artifact Instance of 'AndroidMavenArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'AndroidGenSnapshotArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'AndroidInternalBuildArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'IOSEngineArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'FlutterWebSdk' is not required, skipping update.
[ ] Artifact Instance of 'FlutterSdk' is not required, skipping update.
[ ] Artifact Instance of 'WindowsEngineArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'MacOSEngineArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'LinuxEngineArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'LinuxFuchsiaSDKArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'MacOSFuchsiaSDKArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'FlutterRunnerSDKArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'FlutterRunnerDebugSymbols' is not required, skipping update.
[ ] Artifact Instance of 'IosUsbArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'IosUsbArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'IosUsbArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'IosUsbArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'IosUsbArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'FontSubsetArtifacts' is not required, skipping update.
[ +7 ms] executing: /usr/bin/xcode-select --print-path
[ +7 ms] Exit code 0 from: /usr/bin/xcode-select --print-path
[ ] /Applications/Xcode.app/Contents/Developer
[ +1 ms] executing: /usr/bin/xcodebuild -version
[+1389 ms] Exit code 0 from: /usr/bin/xcodebuild -version
[ ] Xcode 12.5
Build version 12E262
[ +61 ms] executing: sysctl hw.optional.arm64
[ +6 ms] Exit code 1 from: sysctl hw.optional.arm64
[ ] sysctl: unknown oid 'hw.optional.arm64'
[ +2 ms] executing: xcrun xcdevice list --timeout 2
[ +9 ms] xcrun simctl list --json devices
[ ] executing: xcrun simctl list --json devices
[ +194 ms] {
"devices" : {
"com.apple.CoreSimulator.SimRuntime.iOS-14-2" : [
{
"availabilityError" : "runtime profile not found",
"dataPath" :
"/Users/dangkun/Library/Developer/CoreSimulator/Devices/861973AE-1F07-4CD3-80E7-5F1F34BDDC7C/data",
"logPath" : "/Users/dangkun/Library/Logs/CoreSimulator/861973AE-1F07-4CD3-80E7-5F1F34BDDC7C",
"udid" : "861973AE-1F07-4CD3-80E7-5F1F34BDDC7C",
"isAvailable" : false,
"deviceTypeIdentifier" : "com.apple.CoreSimulator.SimDeviceType.iPhone-12",
"state" : "Shutdown",
"name" : "iPhone 12"
}
],
"com.apple.CoreSimulator.SimRuntime.tvOS-14-5" : [

                    ],
                    "com.apple.CoreSimulator.SimRuntime.watchOS-7-4" : [
                      {
                        "dataPath" :
                        "\/Users\/dangkun\/Library\/Developer\/CoreSimulator\/Devices\/42E113B4-0210-4184-8B40-659A8E89D1F6\/data",
                        "logPath" : "\/Users\/dangkun\/Library\/Logs\/CoreSimulator\/42E113B4-0210-4184-8B40-659A8E89D1F6",
                        "udid" : "42E113B4-0210-4184-8B40-659A8E89D1F6",
                        "isAvailable" : true,
                        "deviceTypeIdentifier" : "com.apple.CoreSimulator.SimDeviceType.Apple-Watch-Series-5-44mm",
                        "state" : "Shutdown",
                        "name" : "Apple Watch Series 5 - 44mm"
                      }
                    ],
                    "com.apple.CoreSimulator.SimRuntime.iOS-14-5" : [
                      {
                        "dataPath" :
                        "\/Users\/dangkun\/Library\/Developer\/CoreSimulator\/Devices\/78F214DF-0D5A-43E2-B918-02FD381E84E1\/data",
                        "logPath" : "\/Users\/dangkun\/Library\/Logs\/CoreSimulator\/78F214DF-0D5A-43E2-B918-02FD381E84E1",
                        "udid" : "78F214DF-0D5A-43E2-B918-02FD381E84E1",
                        "isAvailable" : true,
                        "deviceTypeIdentifier" : "com.apple.CoreSimulator.SimDeviceType.iPhone-7",
                        "state" : "Shutdown",
                        "name" : "iPhone 7"
                      },
                      {
                        "dataPath" :
                        "\/Users\/dangkun\/Library\/Developer\/CoreSimulator\/Devices\/9A072928-6F36-4345-B115-AD20956D481C\/data",
                        "logPath" : "\/Users\/dangkun\/Library\/Logs\/CoreSimulator\/9A072928-6F36-4345-B115-AD20956D481C",
                        "udid" : "9A072928-6F36-4345-B115-AD20956D481C",
                        "isAvailable" : true,
                        "deviceTypeIdentifier" : "com.apple.CoreSimulator.SimDeviceType.iPhone-XS-Max",
                        "state" : "Shutdown",
                        "name" : "iPhone Xs Max"
                      },
                      {
                        "dataPath" :
                        "\/Users\/dangkun\/Library\/Developer\/CoreSimulator\/Devices\/0622A1DE-DD97-458C-BF53-87D4B0522AE0\/data",
                        "logPath" : "\/Users\/dangkun\/Library\/Logs\/CoreSimulator\/0622A1DE-DD97-458C-BF53-87D4B0522AE0",
                        "udid" : "0622A1DE-DD97-458C-BF53-87D4B0522AE0",
                        "isAvailable" : true,
                        "deviceTypeIdentifier" : "com.apple.CoreSimulator.SimDeviceType.iPhone-11",
                        "state" : "Shutdown",
                        "name" : "iPhone 11"
                      },
                      {
                        "dataPath" :
                        "\/Users\/dangkun\/Library\/Developer\/CoreSimulator\/Devices\/C20591D4-F8CD-4EAB-8746-BD872021EF65\/data",
                        "logPath" : "\/Users\/dangkun\/Library\/Logs\/CoreSimulator\/C20591D4-F8CD-4EAB-8746-BD872021EF65",
                        "udid" : "C20591D4-F8CD-4EAB-8746-BD872021EF65",
                        "isAvailable" : true,
                        "deviceTypeIdentifier" : "com.apple.CoreSimulator.SimDeviceType.iPhone-12",
                        "state" : "Shutdown",
                        "name" : "iPhone 12"
                      },
                      {
                        "dataPath" :
                        "\/Users\/dangkun\/Library\/Developer\/CoreSimulator\/Devices\/95529135-D442-48C3-BCCA-AE7652876915\/data",
                        "logPath" : "\/Users\/dangkun\/Library\/Logs\/CoreSimulator\/95529135-D442-48C3-BCCA-AE7652876915",
                        "udid" : "95529135-D442-48C3-BCCA-AE7652876915",
                        "isAvailable" : true,
                        "deviceTypeIdentifier" : "com.apple.CoreSimulator.SimDeviceType.iPhone-12-Pro-Max",
                        "state" : "Shutdown",
                        "name" : "iPhone 12 Pro Max"
                      }
                    ],
                    "com.apple.CoreSimulator.SimRuntime.iOS-10-3" : [

                    ]
                  }
                }

[+4251 ms] [
{
"simulator" : true,
"operatingSystemVersion" : "14.5 (18E182)",
"available" : true,
"platform" : "com.apple.platform.iphonesimulator",
"modelCode" : "iPhone13,2",
"identifier" : "C20591D4-F8CD-4EAB-8746-BD872021EF65",
"architecture" : "x86_64",
"modelUTI" : "com.apple.iphone-12-1",
"modelName" : "iPhone 12",
"name" : "iPhone 12"
},
{
"simulator" : true,
"operatingSystemVersion" : "14.5 (18E182)",
"available" : true,
"platform" : "com.apple.platform.iphonesimulator",
"modelCode" : "iPhone12,1",
"identifier" : "0622A1DE-DD97-458C-BF53-87D4B0522AE0",
"architecture" : "x86_64",
"modelUTI" : "com.apple.iphone-11-1",
"modelName" : "iPhone 11",
"name" : "iPhone 11"
},
{
"simulator" : false,
"operatingSystemVersion" : "14.7.1 (18G82)",
"interface" : "usb",
"available" : true,
"platform" : "com.apple.platform.iphoneos",
"modelCode" : "iPhone11,8",
"identifier" : "00008020-0003454C26B8003A",
"architecture" : "arm64e",
"modelUTI" : "com.apple.iphone-xr-2",
"modelName" : "iPhone XR",
"name" : "iPhone (2)"
},
{
"simulator" : true,
"operatingSystemVersion" : "14.5 (18E182)",
"available" : true,
"platform" : "com.apple.platform.iphonesimulator",
"modelCode" : "iPhone13,4",
"identifier" : "95529135-D442-48C3-BCCA-AE7652876915",
"architecture" : "x86_64",
"modelUTI" : "com.apple.iphone-12-pro-max-1",
"modelName" : "iPhone 12 Pro Max",
"name" : "iPhone 12 Pro Max"
},
{
"simulator" : true,
"operatingSystemVersion" : "14.5 (18E182)",
"available" : true,
"platform" : "com.apple.platform.iphonesimulator",
"modelCode" : "iPhone11,4",
"identifier" : "9A072928-6F36-4345-B115-AD20956D481C",
"architecture" : "x86_64",
"modelUTI" : "com.apple.iphone-xs-max-1",
"modelName" : "iPhone Xs Max",
"name" : "iPhone Xs Max"
},
{
"simulator" : true,
"operatingSystemVersion" : "7.4 (18T187)",
"available" : true,
"platform" : "com.apple.platform.watchsimulator",
"modelCode" : "Watch5,4",
"identifier" : "42E113B4-0210-4184-8B40-659A8E89D1F6",
"architecture" : "x86_64",
"modelUTI" : "com.apple.watch-series5-1",
"modelName" : "Apple Watch Series 5 - 44mm",
"name" : "Apple Watch Series 5 - 44mm"
},
{
"simulator" : true,
"operatingSystemVersion" : "14.5 (18E182)",
"available" : true,
"platform" : "com.apple.platform.iphonesimulator",
"modelCode" : "iPhone9,1",
"identifier" : "78F214DF-0D5A-43E2-B918-02FD381E84E1",
"architecture" : "x86_64",
"modelUTI" : "com.apple.iphone-7-1",
"modelName" : "iPhone 7",
"name" : "iPhone 7"
}
]
[ +8 ms] Error: 党坤的iPhone is not connected. Xcode will continue when 党坤的iPhone is connected. (code -13)
[ +50 ms] Checking for advertised Dart observatories...
[+5024 ms] No pointer records found.
[ +2 ms] mDNS query failed. Checking for an interface with a ipv4 link local address.
[ +4 ms] Found interface "en0":
[ ] Bound address: "172.27.101.213"
[ ] Found interface "en7":
[ ] Bound address: "169.254.248.197" link local
[ ] An interface with an ipv4 link local address was found.
[ +6 ms] Waiting for a connection from Flutter on iPhone (2)...</pre>

通过看日志,我们发现,该命令会读取到一个设备list,同时,能看到的是mDNS有一个报错提示。

mDNS是什么呢?mDNS

mDNS协议适用于局域网内没有DNS服务器时的域名解析,设备通过组播的方式交互DNS记录来完成域名解析,约定的组播地址是:224.0.0.251,端口号是5353,mdns协议使用DNS协议一样的数据包,由头部和数据段两部分

从上面的链接中可以了解到,flutter attach通过mDNS来查询调试设备的地址和端口,之后通过http进行通信。

发现问题!

那么有没有可能是因为vscode有缓存信息,导致attach时的目标对象不正确,导致无法attach呢。下面我们来验证一下。

直接使用vscode的attach功能,报错日志如下:

image

同时,原生工程在Flutter的Engine启动后log中会输出Observatory listening url

image

通过对比,我们发现,vscode链接的地址跟原生打印的地址不是同一个,那么问题可以确定是在出在这里了。

解决问题!

原生工程中打印的地址是否可以正确attach呢,我们可以通过命令 Flutter attach --debug-uri=""来验证一下

image

很快就attach成功,就此我们可以愉快的通过命令行进行Hot reload和 Hot restart了

优化方案

然而十分钟后,发现了不对的地方,虽然我们可以attach成功后,可以使用hot reload了,但是因为是通过终端执行的,vscode的断点功能不能使用了。

既然可以终端指定链接的目标,那么能不能通过vscode的配置文件实现呢?

我们看一下vscode的配置文件。

image
image

我们可以通过设置vmServiceUri来指定attach目标。这样我们可以完美的使用attach功能,再也不担心attach报错了。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,126评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,254评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,445评论 0 341
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,185评论 1 278
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,178评论 5 371
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,970评论 1 284
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,276评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,927评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,400评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,883评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,997评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,646评论 4 322
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,213评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,204评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,423评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,423评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,722评论 2 345

推荐阅读更多精彩内容