Last active
May 2, 2024 17:19
-
-
Save eernstg/a1b68a8e1b226a68f4c84dfd8b923d82 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}); |
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
We could also use
const
if nothing else prevents constant construction: