-
Notifications
You must be signed in to change notification settings - Fork 563
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support flutter samples (sample_id) (#2787)
* support flutter samples (sample_id) * review feedback * use switch statement
- Loading branch information
1 parent
982b94f
commit ccc88ca
Showing
3 changed files
with
117 additions
and
39 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'package:http/http.dart' as http; | ||
|
||
class FlutterSampleLoader { | ||
final http.Client client = http.Client(); | ||
|
||
Future<String> loadFlutterSample({ | ||
required String sampleId, | ||
String? channel, | ||
}) async { | ||
// There are only two hosted versions of the docs: master/main and stable. | ||
final sampleUrl = switch (channel) { | ||
'master' => 'https://main-api.flutter.dev/snippets/$sampleId.dart', | ||
'main' => 'https://main-api.flutter.dev/snippets/$sampleId.dart', | ||
_ => 'https://api.flutter.dev/snippets/$sampleId.dart', | ||
}; | ||
|
||
final response = await client.get(Uri.parse(sampleUrl)); | ||
|
||
if (response.statusCode != 200) { | ||
throw Exception('Unable to load sample ' | ||
'(${response.statusCode} ${response.reasonPhrase}})'); | ||
} | ||
|
||
return response.body; | ||
} | ||
|
||
void dispose() { | ||
client.close(); | ||
} | ||
} |
This file contains 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
This file contains 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