Michele Volpato

Michele Volpato

Fix localhost invalid domain in Flutter 1.22

Flutter

Recently, I update Flutter to version 1.22 and one of my apps stopped working on iOS, giving the following error:

Runner[52852:484760] [VERBOSE-2:shell.cc(210)] Dart Unhandled Exception: Invalid argument (domain): Invalid domain name: "localhost", stack trace: #0      new _DomainNetworkPolicy (dart:io/network_policy.dart:85:7)
#1      _constructDomainPolicies (dart:io/network_policy.dart:181:22)
#2      _EmbedderConfig._setDomainPolicies (dart:io/embedder_config.dart:44:23)

It looks like a change in the network policy does not allow to defined localhost as a valid domain.

I am currently using localhost to connect to a local Cloud Functions emulator, by instantiating Cloud Functions with

CloudFunctions(region: region)
    .useFunctionsEmulator(origin: "http://localhost:5001")

and allowing http connection in the Info.plist:

...
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>localhost</key>
        <dict>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
        </dict>
    </dict>
</dict>
...

Apparently this was broken with Flutter 1.22.

My solution was to remove the exception from the Info.plist file, by removing the lines above, and then adding a host to the firebase.json of the local Firebase project, where the emulators are defined:

...
"emulators": {
    "functions": {
        "port": 5001,
        "host": "0.0.0.0" <-- added this
    },
...

This led to an update of instantiation of Cloud Functions in code:

CloudFunctions(region: region)
    .useFunctionsEmulator(origin: ""http://0.0.0.0:5001")

and everything started working again.

There is another change in Flutter 1.23 that might break this again. I will update this short article if needed.

Get a weekly email about Flutter

Subscribe to get a weekly curated list of articles and videos about Flutter and Dart.

    We respect your privacy. Unsubscribe at any time.

    Leave a comment