Skip to content

Commit

Permalink
[cross_file] adopt code excerpts in README (#5347)
Browse files Browse the repository at this point in the history
Updates the README to use a compiled excerpt source for its example of instantiating an `XFile`.

Part of [flutter/flutter#102679](flutter/flutter#102679)
  • Loading branch information
mike-v2 authored Nov 22, 2023
1 parent f2fef4e commit 2102327
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 8 deletions.
3 changes: 2 additions & 1 deletion packages/cross_file/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## NEXT
## 0.3.3+7

* Updates README to improve example of instantiating an XFile.
* Updates minimum supported SDK version to Flutter 3.10/Dart 3.0.

## 0.3.3+6
Expand Down
9 changes: 4 additions & 5 deletions packages/cross_file/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,17 @@ access the file and its metadata.

Example:

<?code-excerpt "example/lib/readme_excerpts.dart (Instantiate)"?>
```dart
import 'package:cross_file/cross_file.dart';
final file = XFile('assets/hello.txt');
final XFile file = XFile('assets/hello.txt');
print('File information:');
print('- Path: ${file.path}');
print('- Name: ${file.name}');
print('- MIME type: ${file.mimeType}');
final fileContent = await file.readAsString();
print('Content of the file: ${fileContent}'); // e.g. "Moto G (4)"
final String fileContent = await file.readAsString();
print('Content of the file: $fileContent');
```

You will find links to the API docs on the [pub page](https://pub.dev/packages/cross_file).
Expand Down
1 change: 1 addition & 0 deletions packages/cross_file/example/assets/hello.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello, world!
24 changes: 24 additions & 0 deletions packages/cross_file/example/lib/readme_excerpts.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// ignore_for_file: avoid_print

import 'package:cross_file/cross_file.dart';

/// Demonstrate instantiating an XFile for the README.
Future<XFile> instantiateXFile() async {
// #docregion Instantiate
final XFile file = XFile('assets/hello.txt');

print('File information:');
print('- Path: ${file.path}');
print('- Name: ${file.name}');
print('- MIME type: ${file.mimeType}');

final String fileContent = await file.readAsString();
print('Content of the file: $fileContent');
// #enddocregion Instantiate

return file;
}
18 changes: 18 additions & 0 deletions packages/cross_file/example/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: cross_file_example
description: Demonstrates how to use cross files.
publish_to: none

environment:
sdk: ">=3.0.0 <4.0.0"

dependencies:
cross_file:
# When depending on this package from a real application you should use:
# cross_file: ^x.y.z
# See https://dart.dev/tools/pub/dependencies#version-constraints
# The example app is bundled with the plugin so we use a path dependency on
# the parent directory to use the current plugin's version.
path: ../

dev_dependencies:
test: ^1.24.0
23 changes: 23 additions & 0 deletions packages/cross_file/example/test/readme_excerpts_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2013 The Flutter Authors. 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:cross_file/cross_file.dart';
import 'package:cross_file_example/readme_excerpts.dart';
import 'package:test/test.dart';

const bool kIsWeb = bool.fromEnvironment('dart.library.js_util');

void main() {
test('instantiateXFile loads asset file', () async {
// Ensure that the snippet code runs successfully.
final XFile xFile = await instantiateXFile();
// It should have a nonempty path and name.
expect(xFile.path, allOf(isNotNull, isNotEmpty));
expect(xFile.name, allOf(isNotNull, isNotEmpty));

// And the example file should have contents.
final String fileContent = await xFile.readAsString();
expect(fileContent, allOf(isNotNull, isNotEmpty));
}, skip: kIsWeb);
}
2 changes: 1 addition & 1 deletion packages/cross_file/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: cross_file
description: An abstraction to allow working with files across multiple platforms.
repository: https://github.com/flutter/packages/tree/main/packages/cross_file
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+cross_file%22
version: 0.3.3+6
version: 0.3.3+7

environment:
sdk: ">=3.0.0 <4.0.0"
Expand Down
1 change: 0 additions & 1 deletion script/configs/temp_exclude_excerpt.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

# TODO(stuartmorgan): Remove everything from this list. See
# https://github.com/flutter/flutter/issues/102679
- cross_file
- css_colors
- espresso
- extension_google_sign_in_as_googleapis_auth
Expand Down

0 comments on commit 2102327

Please sign in to comment.