Skip to content

Instantly share code, notes, and snippets.

@eernstg
Last active May 2, 2024 17:19
Show Gist options
  • Save eernstg/a1b68a8e1b226a68f4c84dfd8b923d82 to your computer and use it in GitHub Desktop.
Save eernstg/a1b68a8e1b226a68f4c84dfd8b923d82 to your computer and use it in GitHub Desktop.
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:json/json.dart';
main() async {
const pubUrl = "https://pub.dartlang.org/api/packages/protobuf";
var response = await http.get(Uri.parse(pubUrl));
if (response.statusCode == 200) {
PkgInfo info = PkgInfo.fromJson(json.decode(response.body));
print('Package ${info.name}, latest is v${info.latest.pubspec.version}');
print(info.latest.pubspec.description);
} else {
throw Exception('Failed to load package info');
}
}
@JsonCodable()
class PkgInfo({required final String name, required final PkgVersion latest});
@JsonCodable()
class PkgVersion({required final String version, required final PkgPubspec pubspec});
@JsonCodable()
class PkgPubspec({required final String version, required final String description});
@eernstg
Copy link
Author

eernstg commented May 2, 2024

We could also use const if nothing else prevents constant construction:

@JsonCodable()
class const PkgInfo({required String name, required PkgVersion latest});

@JsonCodable()
class const PkgVersion({required String version, required PkgPubspec pubspec});

@JsonCodable()
class const PkgPubspec({required String version, required String description});

@eernstg
Copy link
Author

eernstg commented May 2, 2024

Oh, and if we make it possible to omit required in concrete function signatures (not function types, not abstract method declarations) in the case where the formal parameter type is non-nullable then we get this:

@JsonCodable()
class const PkgInfo({String name, PkgVersion latest});

@JsonCodable()
class const PkgVersion({String version, PkgPubspec pubspec});

@JsonCodable()
class const PkgPubspec({String version, String description});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment