iTranslated by AI
Grinder command to launch Flutter Web for local network access
Introduction
When developing for Flutter Web, you may want to check the result on your smartphone or other devices.
I created a useful Grinder command for such occasions.
Prerequisites
How to check Flutter Web from a local network
https://zenn.dev/fastriver/articles/flutter-web-debug-lan
What is Grinder?
https://zenn.dev/k9i/articles/bcfa83b08e56d6
What I want to achieve
- Get the device's IP address
- Run it with the web-server option
Source Code
Please refer to the articles in the prerequisites for how to use Grinder.
The fw function is the core. It extracts the IPv4 address from the results of the ifconfig command and executes flutter run based on that.
import 'dart:io';
import 'package:grinder/grinder.dart';
void main(List<String> args) => grind(args);
Future<void> _runCommand({
required String command,
}) async {
final splittedCommand = command.split(' ');
log(command);
final process = await Process.start(
splittedCommand.first,
splittedCommand.sublist(1),
);
stdout.addStream(process.stdout);
stderr.addStream(process.stderr);
}
@Task('flutter run -d web-server --web-hostname=0.0.0.0 --web-port=50505')
void fw() {
final result = Process.runSync('ifconfig', [], runInShell: true);
if (result.exitCode != 0) {
throw Exception(
'ifconfig command failed with exit code ${result.exitCode}');
}
final output = result.stdout.toString();
final lines = output.split('\n');
for (var line in lines) {
if (line.contains('inet ') && !line.contains('127.0.0.1')) {
final ip = line.split(' ')[1];
log(ip);
_runCommand(
command:
'flutter run -d web-server --web-hostname=$ip --web-port=50505',
);
break;
}
}
}
```text
## Running the Command
Run the following command in the terminal:
```zsh
grind fw
The server is up and running! 🥳
By accessing http://192.168.181.67:50505, you can see the Flutter Web from devices within the network.

Summary
Now you can check your Flutter Web app from your local network simply by entering grind fw.
Postscript
It seems you can also start it with the following command using a wildcard mask (IP address 0.0.0.0) without having to obtain the IP address yourself. If you have a fixed IP address, you could use this to start it up and access that fixed address.
flutter run -d web-server --web-hostname=0.0.0.0 --web-port=50505
Discussion