Skip to content

Commit f8b2412

Browse files
authored
Update Dart sample to Dart 3 (#5663)
* Update Dart sample to Dart 3 * Update Dart SDK mention
1 parent 6a5e9b9 commit f8b2412

File tree

4 files changed

+24
-68
lines changed

4 files changed

+24
-68
lines changed
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# See https://www.dartlang.org/guides/libraries/private-files
2-
.dart_tool
3-
.packages
1+
# https://dart.dev/guides/libraries/private-files
2+
.dart_tool/
43
pubspec.lock

code-samples/community/serving/helloworld-dart/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ that you can use for testing. It reads in the env variable `TARGET` and prints
88
## Prerequisites
99

1010
- A Kubernetes cluster with Knative installed and DNS configured. Follow the
11-
[Knative installation instructions](https://knative.dev/docs/install/) if you need to create
12-
one.
11+
[Knative installation instructions](https://knative.dev/docs/install/) if
12+
you need to create one.
1313
- [Docker](https://www.docker.com) installed and running on your local machine,
1414
and a Docker Hub account configured (we'll use it for a container registry).
15-
- [dart-sdk](https://www.dart.dev/tools/sdk#install) installed and
15+
- The [Dart SDK](https://dart.dev/get-dart) installed and
1616
configured if you want to run the program locally.
1717

1818
## Recreating the sample code

code-samples/community/serving/helloworld-dart/index.md

Lines changed: 13 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -8,76 +8,33 @@ that you can use for testing. It reads in the env variable `TARGET` and prints
88
## Prerequisites
99

1010
- A Kubernetes cluster with Knative installed and DNS configured. Follow the
11-
[Knative installation instructions](https://knative.dev/docs/install/) if you need to create
12-
one.
11+
[Knative installation instructions](https://knative.dev/docs/install/) if
12+
you need to create one.
1313
- [Docker](https://www.docker.com) installed and running on your local machine,
1414
and a Docker Hub account configured (we'll use it for a container registry).
15-
- [dart-sdk](https://www.dart.dev/tools/sdk#install) installed and
15+
- The [Dart SDK](https://dart.dev/get-dart) installed and
1616
configured if you want to run the program locally.
1717

1818
## Recreating the sample code
1919

20-
While you can clone all of the code from this directory, it is useful to know
21-
how to build a hello world Dart application step-by-step. This application can
22-
be created using the following instructions.
20+
While you can clone all of the code from this directory, we recommend you create
21+
your hello world Dart application by using the `dart` developer tool. This takes
22+
just a few steps:
2323

24-
1. Create a new directory and write `pubspec.yaml` as follows:
24+
1. Create a new Dart app using the `server_shelf` template:
2525

26-
```yaml
27-
name: hello_world_dart
28-
publish_to: none # let's not accidentally publish this to pub.dartlang.org
29-
30-
environment:
31-
sdk: ">=2.12.0 <3.0.0"
32-
33-
dependencies:
34-
shelf: ^1.0.0
26+
```shell
27+
> dart create -t server-shelf helloworld-dart
3528
```
3629

37-
2. If you want to run locally, install dependencies. If you only want to run in
30+
1. If you want to run locally, install dependencies. If you only want to run in
3831
Docker or Knative, you can skip this step.
3932

40-
```bash
41-
> pub get
42-
```
43-
44-
3. Create a new file `bin/server.dart` and write the following code:
45-
46-
```dart
47-
import 'dart:io';
48-
49-
import 'package:shelf/shelf.dart';
50-
import 'package:shelf/shelf_io.dart';
51-
52-
Future main() async {
53-
// Find port to listen on from environment variable.
54-
final port = int.parse(Platform.environment['PORT'] ?? '8080');
55-
56-
// Read $TARGET from environment variable.
57-
final target = Platform.environment['TARGET'] ?? 'World';
58-
59-
Response handler(Request request) => Response.ok('Hello $target');
60-
61-
// Serve handler on given port.
62-
final server = await serve(
63-
const Pipeline().addMiddleware(logRequests()).addHandler(handler),
64-
InternetAddress.anyIPv4,
65-
port,
66-
);
67-
print('Serving at http://${server.address.host}:${server.port}');
68-
}
69-
```
70-
71-
4. Create a new file named `Dockerfile`, this file defines instructions for
72-
dockerizing your applications, for dart apps this can be done as follows:
73-
74-
```Dockerfile
75-
# Use Google's official Dart image.
76-
# https://hub.docker.com/r/google/dart-runtime/
77-
FROM google/dart-runtime
33+
```shell
34+
> dart pub get
7835
```
7936

80-
5. Create a new file, `service.yaml` and copy the following service definition
37+
1. Create a new file, `service.yaml` and copy the following service definition
8138
into the file. Make sure to replace `{username}` with your Docker Hub
8239
username.
8340

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
name: hello_world_dart
2-
publish_to: none # let's not accidentally publish this to pub.dartlang.org
2+
publish_to: none # Avoid accidentally publishing this to pub.dev
33

44
environment:
5-
sdk: ">=2.17.0 <3.0.0"
5+
sdk: ^3.0.0
66

77
dependencies:
8-
args: ^2.0.0
9-
shelf: ^1.1.0
10-
shelf_router: ^1.0.0
8+
args: ^2.3.0
9+
shelf: ^1.4.0
10+
shelf_router: ^1.1.0
1111

1212
dev_dependencies:
1313
http: ^0.13.0
1414
lints: ^2.0.0
15-
test: ^1.15.0
15+
test: ^1.21.0

0 commit comments

Comments
 (0)