-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathresource_loader.dart
33 lines (26 loc) · 1.05 KB
/
resource_loader.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Copyright (c) 2015, 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.
/// Make it possible to load resources, independent of how the Dart app is run.
///
/// Future<String> getTemplateFile(String templatePath) {
/// return loadAsString('package:dartdoc/templates/$templatePath');
/// }
///
library dartdoc.resource_loader;
import 'dart:async' show Future;
import 'dart:convert' show utf8;
import 'package:resource/resource.dart' as resource;
/// Loads a `package:` resource as a String.
Future<String> loadAsString(String path) async {
var bytes = await loadAsBytes(path);
return utf8.decode(bytes);
}
/// Loads a `package:` resource as an [List<int>].
Future<List<int>> loadAsBytes(String path) async {
if (!path.startsWith('package:')) {
throw ArgumentError('path must begin with package:');
}
var uri = Uri.parse(path);
return await resource.ResourceLoader.defaultLoader.readAsBytes(uri);
}