-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
161 changed files
with
11,107 additions
and
531 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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
include: package:pedantic/analysis_options.1.8.0.yaml | ||
|
||
analyzer: | ||
exclude: [build/**] | ||
strong-mode: | ||
implicit-casts: false | ||
|
||
linter: | ||
# Rules and documentation: http://dart-lang.github.io/linter/lints | ||
rules: | ||
- annotate_overrides | ||
- await_only_futures | ||
- camel_case_types | ||
- cancel_subscriptions | ||
- close_sinks | ||
- comment_references | ||
- constant_identifier_names | ||
- control_flow_in_finally | ||
- empty_statements | ||
- hash_and_equals | ||
- implementation_imports | ||
- iterable_contains_unrelated_type | ||
- list_remove_unrelated_type | ||
- non_constant_identifier_names | ||
- one_member_abstracts | ||
- only_throw_errors | ||
- overridden_fields | ||
- package_api_docs | ||
- package_names | ||
- package_prefixed_library_names | ||
- sort_constructors_first | ||
- sort_unnamed_constructors_first | ||
- test_types_in_equals | ||
- throw_in_finally | ||
- type_annotate_public_apis | ||
- unnecessary_brace_in_string_interps | ||
- unnecessary_const | ||
- unnecessary_getters_setters | ||
- unnecessary_new |
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 @@ | ||
include: ../analysis_options.yaml |
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,25 @@ | ||
Future<void> printOrderMessage() async { | ||
print('Awaiting user order...'); | ||
// #docregion swap-stmts | ||
var order = await fetchUserOrder(); | ||
// print('Awaiting user order...'); | ||
// #enddocregion swap-stmts | ||
print('Your order is: $order'); | ||
} | ||
|
||
Future<String> fetchUserOrder() { | ||
// Imagine that this function is more complex and slow. | ||
return Future.delayed(Duration(seconds: 4), () => 'Large Latte'); | ||
} | ||
|
||
Future<void> main() async { | ||
countSeconds(4); | ||
await printOrderMessage(); | ||
} | ||
|
||
// You can ignore this function - it's here to visualize delay time in this example. | ||
void countSeconds(int s) { | ||
for (var i = 1; i <= s; i++) { | ||
Future.delayed(Duration(seconds: i), () => print(i)); | ||
} | ||
} |
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,19 @@ | ||
// #docregion '' | ||
Future<void> fetchUserOrder() { | ||
// Imagine that this function is fetching user info from another service or database. | ||
return Future.delayed(Duration(seconds: 2), () => print('Large Latte')); | ||
} | ||
// #enddocregion '' | ||
|
||
// #docregion error | ||
Future<void> fetchUserOrderError() { | ||
// Imagine that this function is fetching user info but encounters a bug | ||
return Future.delayed(Duration(seconds: 2), | ||
() => throw Exception('Logout failed: user ID is invalid')); | ||
} | ||
// #docregion '' | ||
|
||
void main() { | ||
fetchUserOrder(); | ||
print('Fetching user order...'); | ||
} |
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,20 @@ | ||
Future<String> createOrderMessage() async { | ||
var order = await fetchUserOrder(); | ||
return 'Your order is: $order'; | ||
} | ||
|
||
Future<String> fetchUserOrder() => | ||
// Imagine that this function is more complex and slow. | ||
Future.delayed( | ||
Duration(seconds: 2), | ||
() => 'Large Latte', | ||
); | ||
|
||
// #docregion main-sig | ||
Future<void> main() async { | ||
// #enddocregion main-sig | ||
print('Fetching user order...'); | ||
// #docregion print-order | ||
print(await createOrderMessage()); | ||
// #enddocregion print-order | ||
} |
21 changes: 21 additions & 0 deletions
21
null_safety_examples/async_await/bin/get_order_sync_bad.dart
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,21 @@ | ||
// This example shows how *not* to write asynchronous Dart code. | ||
|
||
// #docregion no-warning | ||
String createOrderMessage() { | ||
var order = fetchUserOrder(); | ||
return 'Your order is: $order'; | ||
} | ||
|
||
Future<String> fetchUserOrder() => | ||
// Imagine that this function is more complex and slow. | ||
Future.delayed( | ||
Duration(seconds: 2), | ||
() => 'Large Latte', | ||
); | ||
|
||
// #docregion main-sig | ||
void main() { | ||
// #enddocregion main-sig | ||
print('Fetching user order...'); | ||
print(createOrderMessage()); | ||
} |
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,24 @@ | ||
Future<void> printOrderMessage() async { | ||
// #docregion try-catch | ||
try { | ||
var order = await fetchUserOrder(); | ||
print('Awaiting user order...'); | ||
print(order); | ||
} catch (err) { | ||
print('Caught error: $err'); | ||
} | ||
// #enddocregion try-catch | ||
} | ||
|
||
Future<String> fetchUserOrder() { | ||
// Imagine that this function is more complex. | ||
var str = Future.delayed( | ||
Duration(seconds: 4), | ||
// ignore: only_throw_errors | ||
() => throw 'Cannot locate user order'); | ||
return str; | ||
} | ||
|
||
Future<void> main() async { | ||
await printOrderMessage(); | ||
} |
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 @@ | ||
include: ../dart_test_base_browser.yaml |
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,10 @@ | ||
name: async_await | ||
description: dart.dev example code. | ||
|
||
environment: | ||
sdk: ">=2.12.0-0 <3.0.0" | ||
|
||
dev_dependencies: | ||
examples_util: {path: ../util} | ||
pedantic: ^1.10.0-nullsafety.3 | ||
test: ^1.16.0-nullsafety.13 |
55 changes: 55 additions & 0 deletions
55
null_safety_examples/async_await/test/async_await_test.dart
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,55 @@ | ||
import 'package:test/test.dart'; | ||
import 'package:examples_util/print_matcher.dart' as m; | ||
|
||
import '../bin/async_example.dart' as async_example; | ||
import '../bin/futures_intro.dart' as futures_intro; | ||
import '../bin/get_order_sync_bad.dart' as get_order_sync_bad; | ||
import '../bin/get_order.dart' as get_order; | ||
import '../bin/try_catch.dart' as try_catch; | ||
|
||
void main() { | ||
test('async_example', () { | ||
final output = ''' | ||
Awaiting user order... | ||
1 | ||
2 | ||
3 | ||
4 | ||
Your order is: Large Latte | ||
'''; | ||
expect(async_example.main, m.printsLines(output)); | ||
}); | ||
|
||
test('futures_intro', () { | ||
final output = ''' | ||
Fetching user order... | ||
Large Latte | ||
'''; | ||
expect( | ||
() => Future.wait([ | ||
Future.delayed(Duration(seconds: 4)), | ||
Future.sync(futures_intro.main), | ||
]), | ||
m.printsLines(output)); | ||
}); | ||
|
||
test('get_order_sync_bad', () { | ||
final output = ''' | ||
Fetching user order... | ||
Your order is: Instance of 'Future<String>' | ||
'''; | ||
expect(get_order_sync_bad.main, m.printsLines(output)); | ||
}); | ||
|
||
test('get_order', () { | ||
final output = ''' | ||
Fetching user order... | ||
Your order is: Large Latte | ||
'''; | ||
expect(get_order.main, m.printsLines(output)); | ||
}); | ||
|
||
test('try_catch', () { | ||
expect(try_catch.main, m.prints('Caught error: Cannot locate user order')); | ||
}); | ||
} |
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,2 @@ | ||
# When analyzing build/test logs, the expanded reporter is better: | ||
reporter: expanded |
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,13 @@ | ||
include: dart_test_base.yaml | ||
|
||
tags: | ||
browser: | ||
|
||
# Chrome option required because Travis is't running under sudo | ||
# https://docs.travis-ci.com/user/chrome#Sandboxing | ||
define_platforms: | ||
travischrome: | ||
name: Chrome for Travis w/o sudo | ||
extends: chrome | ||
settings: | ||
arguments: --no-sandbox |
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 @@ | ||
include: ../analysis_options.yaml |
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,10 @@ | ||
name: iterables | ||
description: dart.dev example code. | ||
|
||
environment: | ||
sdk: ">=2.12.0-0 <3.0.0" | ||
|
||
dev_dependencies: | ||
examples_util: {path: ../util} | ||
pedantic: ^1.10.0-nullsafety.3 | ||
test: ^1.16.0-nullsafety.13 |
Oops, something went wrong.