Adding port finder from flutter tool.
diff --git a/lib/src/flutter_web.dart b/lib/src/flutter_web.dart
index 5ddcbd7..90d3561 100644
--- a/lib/src/flutter_web.dart
+++ b/lib/src/flutter_web.dart
@@ -17,10 +17,6 @@
class FlutterWebManager {
final FlutterSdk flutterSdk;
- // TODO(redbrogdon): Find a better way to determine the number of an unused
- // port.
- static const observatoryPort = 49003;
-
Directory _projectDirectory;
bool _initedFlutterWeb = false;
@@ -139,6 +135,8 @@
Future<void> _runPubGet() async {
_logger.info('running flutter pub get (${_projectDirectory.path})');
+ final observatoryPort = await _findFreePort();
+
// The DART_VM_OPTIONS flag is included here to override the one sent by the
// Dart SDK during tests. Without the flag, the Flutter tool will attempt to
// spin up its own observatory on the same port as the one already
@@ -164,6 +162,33 @@
}
}
+ Future<int> _findFreePort({bool ipv6 = false}) async {
+ var port = 0;
+ ServerSocket serverSocket;
+ final InternetAddress loopback =
+ ipv6 ? InternetAddress.loopbackIPv6 : InternetAddress.loopbackIPv4;
+
+ try {
+ serverSocket = await ServerSocket.bind(loopback, 0);
+ port = serverSocket.port;
+ } on SocketException catch (e) {
+ // If ipv4 loopback bind fails, try ipv6.
+ if (!ipv6) {
+ return _findFreePort(ipv6: true);
+ }
+ _logger.severe('Could not find free port for `pub get`: $e');
+ } catch (e) {
+ // Failures are signaled by a return value of 0 from this function.
+ _logger.severe('Could not find free port for `pub get`: $e');
+ } finally {
+ if (serverSocket != null) {
+ await serverSocket.close();
+ }
+ }
+
+ return port;
+ }
+
static const String _samplePackageName = 'dartpad_sample';
static String createPubspec(bool includeFlutterWeb) {