unpub
unpub copied to clipboard
Implementing null safety in app.serve
In Dart 2.17.6, unpub is failing at compilation.
/Users/vi/flutter/bin/cache/dart-sdk/bin/dart --enable-asserts /Users/vi/IdeaProjects/unpub/unpub/bin/unpub.dart lib/src/app.dart:130:48: Error: The argument type 'String?' can't be assigned to the parameter type 'Object' because 'String?' is nullable and 'Object' isn't.
- 'Object' is from 'dart:core'. var server = await shelf_io.serve(handler, host, port); ^
Process finished with exit code 254
Adding proper null safety in app.dart to avoid this failure.
I would rather change only the serv function, since when you access the parameter map in unpub.dart, it may return null:
-Future<HttpServer> serve([String? host = '0.0.0.0', int port = 4000]) async {
+Future<HttpServer> serve([String? host, int port = 4000]) async {
var handler = const shelf.Pipeline()
.addMiddleware(corsHeaders())
.addMiddleware(shelf.logRequests())
.addHandler((req) async {
// Return 404 by default
// https://github.com/google/dart-neats/issues/1
var res = await router.call(req);
return res;
});
- var server = await shelf_io.serve(handler, host, port);
+ var server = await shelf_io.serve(handler, host ?? '0.0.0.0', port);
return server;
}
Referring to: https://github.com/bytedance/unpub/blob/5a7aaa04b76f8bfb06dd3758581eb401ef07e2db/unpub/lib/src/app.dart#L120-L132
I think this also matches the authors intent better, he probably just misunderstood that default values would not be applied, when you pass null to it. Everyone makes mistakes!