Sử dụng câu lệnh
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
68 để chỉ định một khối mã JavaScript sẽ được thực thi nếu một điều kiện là đúngcú pháp
if [điều kiện] {
// khối mã sẽ được thực thi nếu điều kiện là đúng
}
Lưu ý rằng
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
68 là chữ thường. Chữ in hoa [If hoặc IF] sẽ tạo ra lỗi JavaScriptThí dụ
Thực hiện lời chúc "Chúc một ngày tốt lành" nếu chưa đến 18 giờ. 00
if [giờ < 18] {
lời chào = "Chúc một ngày tốt lành";
}
Kết quả của lời chào sẽ là
Tự mình thử »Tuyên bố khác
Sử dụng câu lệnh
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
70 để chỉ định một khối mã sẽ được thực thi nếu điều kiện saiif [điều kiện] {
// khối mã sẽ được thực thi nếu điều kiện đúng
} else {
// block of code to be executed if the condition is false
}
Thí dụ
Nếu chưa đến 18 giờ thì tạo lời chào "Chúc một ngày tốt lành", nếu không thì "Chào buổi tối"
if [hour < 18] {
greeting = "Chúc một ngày tốt lành";
} other {
lời chào
}
Kết quả của lời chào sẽ là
Tự mình thử »Câu lệnh if khác
Sử dụng câu lệnh
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
71 để chỉ định một điều kiện mới nếu điều kiện đầu tiên là saicú pháp
if [condition1] {
// khối mã sẽ được thực thi nếu điều kiện 1 đúng
} else if [condition2] {
// block of code to be executed if the condition1 is false and condition2 is true
} else {
// block of code to be executed if the condition1 is false and condition2 is false
}
Thí dụ
Nếu thời gian nhỏ hơn 10. 00 thì tạo lời chào "Chào buổi sáng", nếu chưa có nhưng thời gian dưới 20. 00, tạo lời chào "Chúc một ngày tốt lành", nếu không là "Chào buổi tối"
Lớp học lập trình này hướng dẫn bạn cách viết mã không đồng bộ bằng cách sử dụng hợp đồng tương lai và các từ khóa
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
6 và Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
7. Sử dụng trình chỉnh sửa DartPad nhúng, bạn có thể kiểm tra kiến thức của mình bằng cách chạy mã ví dụ và hoàn thành bài tậpĐể tận dụng tối đa codelab này, bạn nên có những điều sau đây
- Kiến thức về cú pháp Dart cơ bản
- Một số kinh nghiệm viết mã không đồng bộ bằng ngôn ngữ khác
Codelab này bao gồm các tài liệu sau
- Cách thức và thời điểm sử dụng các từ khóa
6 vàFuture fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
7Future fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
- Việc sử dụng
6 vàFuture fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
7 ảnh hưởng đến thứ tự thực hiện như thế nàoFuture fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
- Cách xử lý lỗi từ lệnh gọi không đồng bộ bằng cách sử dụng biểu thức
2 trong hàmvoid main[] async { ··· }
6Future fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
Thời gian ước tính để hoàn thành phòng thí nghiệm mã này. 40-60 phút
Ghi chú. Trang này sử dụng DartPads được nhúng để hiển thị các ví dụ và bài tập. Nếu bạn thấy các ô trống thay vì DartPad, hãy truy cập trang xử lý sự cố DartPad
Tại sao mã không đồng bộ lại quan trọng
Hoạt động không đồng bộ cho phép chương trình của bạn hoàn thành công việc trong khi chờ hoạt động khác kết thúc. Dưới đây là một số hoạt động không đồng bộ phổ biến
- Tìm nạp dữ liệu qua mạng
- Ghi vào cơ sở dữ liệu
- Đọc dữ liệu từ một tập tin
Các tính toán không đồng bộ như vậy thường cung cấp kết quả dưới dạng
void main[] async { ··· }
4 hoặc, nếu kết quả có nhiều phần, dưới dạng void main[] async { ··· }
5. Những tính toán này giới thiệu sự không đồng bộ vào một chương trình. Để phù hợp với sự không đồng bộ ban đầu đó, các hàm Dart đơn giản khác cũng cần trở nên không đồng bộĐể tương tác với các kết quả không đồng bộ này, bạn có thể sử dụng từ khóa
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
6 và Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
7. Hầu hết các hàm không đồng bộ chỉ là các hàm Dart không đồng bộ, có thể phụ thuộc sâu vào tính toán không đồng bộ vốn cóThí dụ. Sử dụng sai chức năng không đồng bộ
Ví dụ sau đây cho thấy cách sai khi sử dụng hàm không đồng bộ [_______68]. Sau này bạn sẽ sửa ví dụ bằng cách sử dụng
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
6 và Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
7. Trước khi chạy ví dụ này, hãy thử phát hiện vấn đề – bạn nghĩ đầu ra sẽ như thế nào?Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
5Đây là lý do tại sao ví dụ không in được giá trị mà
void main[] async { ··· }
8 cuối cùng tạo ra
8 là một hàm không đồng bộ, sau một khoảng thời gian trễ, sẽ cung cấp một chuỗi mô tả đơn đặt hàng của người dùng. một “Latte lớn”void main[] async { ··· }
- Để nhận được đơn đặt hàng của người dùng, ________ 183 nên gọi cho ________ 68 và đợi kết thúc. Vì
83 không đợi cho đến khiFuture fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
8 kết thúc, nênvoid main[] async { ··· }
83 không nhận được giá trị chuỗi mà cuối cùngFuture fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
8 đã cung cấpvoid main[] async { ··· }
- Thay vào đó,
83 nhận được một đại diện của công việc đang chờ xử lý được thực hiện. một tương lai chưa hoàn thành. Bạn sẽ tìm hiểu thêm về tương lai trong phần tiếp theoFuture fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
- Because
83 fails to get the value describing the user’s order, the example fails to print “Large Latte” to the console, and instead prints “Your order is: Instance of ‘_Future’”.Future fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
Trong các phần tiếp theo, bạn sẽ tìm hiểu về hợp đồng tương lai và cách làm việc với hợp đồng tương lai [sử dụng
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
6 và Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
7] để bạn có thể viết mã cần thiết để làm cho void main[] async { ··· }
8 in giá trị mong muốn [“Latte Latte lớn”] vào bảng điều khiểnĐiều khoản quan trọng
- hoạt động đồng bộ. Một hoạt động đồng bộ chặn các hoạt động khác thực thi cho đến khi nó hoàn thành
- chức năng đồng bộ. Hàm đồng bộ chỉ thực hiện các hoạt động đồng bộ
- hoạt động không đồng bộ. Sau khi bắt đầu, một hoạt động không đồng bộ cho phép các hoạt động khác thực thi trước khi hoàn thành
- chức năng không đồng bộ. Hàm không đồng bộ thực hiện ít nhất một thao tác không đồng bộ và cũng có thể thực hiện các thao tác đồng bộ
Tương lai là gì?
Tương lai [viết thường “f”] là một thể hiện của lớp Tương lai [viết hoa “F”]. Một tương lai đại diện cho kết quả của một hoạt động không đồng bộ và có thể có hai trạng thái. chưa hoàn thành hoặc đã hoàn thành
Ghi chú. Chưa hoàn thành là một thuật ngữ Dart đề cập đến trạng thái của một tương lai trước khi nó tạo ra một giá trị
chưa hoàn thành
Khi bạn gọi một hàm không đồng bộ, nó sẽ trả về một tương lai chưa hoàn thành. Tương lai đó đang chờ hoạt động không đồng bộ của chức năng kết thúc hoặc đưa ra lỗi
Hoàn thành
Nếu hoạt động không đồng bộ thành công, tương lai sẽ hoàn thành với một giá trị. Mặt khác, nó hoàn thành với một lỗi
Hoàn thành với một giá trị
Một tương lai của loại
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
94 hoàn thành với một giá trị của loại Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
95. Ví dụ: một tương lai với loại Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
96 tạo ra một giá trị chuỗi. Nếu một tương lai không tạo ra giá trị sử dụng được, thì loại tương lai là Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
97Hoàn thành với một lỗi
Nếu hoạt động không đồng bộ được thực hiện bởi chức năng không thành công vì bất kỳ lý do gì, tương lai sẽ hoàn thành với một lỗi
Thí dụ. giới thiệu tương lai
Trong ví dụ sau,
void main[] async { ··· }
8 trả về một tương lai hoàn thành sau khi in ra bàn điều khiển. Bởi vì nó không trả về giá trị có thể sử dụng, nên void main[] async { ··· }
8 có kiểu Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
97. Trước khi bạn chạy ví dụ, hãy thử dự đoán cái nào sẽ in trước. “Latte Latte lớn” hoặc “Đang tìm nạp đơn hàng của người dùng…”Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
6Trong ví dụ trước, mặc dù
void main[] async { ··· }
8 thực thi trước lệnh gọi Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
82 trên dòng 8, bảng điều khiển hiển thị đầu ra từ dòng 8[“Tìm nạp đơn đặt hàng của người dùng…”] trước đầu ra từ void main[] async { ··· }
8 [“Latte Latte lớn”]. Điều này là do sự chậm trễ của void main[] async { ··· }
8 trước khi nó in “Large Latte”Thí dụ. Hoàn thành với một lỗi
Chạy ví dụ sau để xem cách một tương lai hoàn thành có lỗi. Một lúc sau, bạn sẽ học cách xử lý lỗi
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
Trong ví dụ này,
void main[] async { ··· }
8 hoàn tất với lỗi cho biết ID người dùng không hợp lệBạn đã học về hợp đồng tương lai và cách chúng hoàn thành, nhưng bạn sử dụng kết quả của các hàm không đồng bộ như thế nào?
đánh giá nhanh
- A Future instance produces a value of type
95.Future fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
- Nếu một tương lai không tạo ra giá trị sử dụng được, thì loại tương lai là
97Future fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
- Một tương lai có thể ở một trong hai trạng thái. chưa hoàn thành hoặc đã hoàn thành
- Khi bạn gọi một hàm trả về một tương lai, hàm này sẽ xếp hàng công việc cần hoàn thành và trả về một tương lai chưa hoàn thành
- Khi hoạt động của tương lai kết thúc, tương lai hoàn thành với một giá trị hoặc có lỗi
Điều khoản quan trọng
- Tương lai. lớp phi tiêu tương lai
- Tương lai. một thể hiện của lớp Dart
4void main[] async { ··· }
Làm việc với tương lai. không đồng bộ và chờ đợi
Các từ khóa
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
6 và Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
7 cung cấp một cách khai báo để xác định các hàm không đồng bộ và sử dụng kết quả của chúng. Hãy nhớ hai hướng dẫn cơ bản này khi sử dụng Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
6 và Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
7- Để xác định hàm không đồng bộ, hãy thêm
6 trước thân hàmFuture fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
- Từ khóa
7 chỉ hoạt động trong các hàmFuture fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
6Future fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
Đây là một ví dụ chuyển đổi
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
58 từ chức năng đồng bộ sang không đồng bộĐầu tiên, thêm từ khóa
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
6 trước thân hàmvoid main[] async { ··· }
Nếu hàm có kiểu trả về được khai báo, thì hãy cập nhật kiểu đó thành
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
94, trong đó Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
95 là kiểu giá trị mà hàm trả về. Nếu hàm không trả về một giá trị rõ ràng, thì kiểu trả về là Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
97Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
8Bây giờ bạn đã có hàm
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
6, bạn có thể sử dụng từ khóa Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
7 để đợi một tương lai hoàn thànhFuture fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
9Như hai ví dụ sau cho thấy, các từ khóa
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
6 và Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
7 dẫn đến mã không đồng bộ trông rất giống mã đồng bộ. Sự khác biệt duy nhất được đánh dấu trong ví dụ không đồng bộ, mà—nếu cửa sổ của bạn đủ rộng—nằm ở bên phải của ví dụ đồng bộThí dụ. chức năng đồng bộ
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
8Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
5Thí dụ. chức năng không đồng bộ
void main[] async { ··· }
9Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
0Ví dụ không đồng bộ khác nhau theo ba cách
- Kiểu trả về cho
83 thay đổi từFuture fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
98 thànhvoid main[] async { ··· }
96Future fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
- Từ khóa
6 xuất hiện trước thân hàm choFuture fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
83 vàFuture fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
58Future fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
- Từ khóa
7 xuất hiện trước khi gọi các hàm không đồng bộFuture fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
8 vàvoid main[] async { ··· }
83Future fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
Điều khoản quan trọng
- không đồng bộ. Bạn có thể sử dụng từ khóa
6 trước phần thân của hàm để đánh dấu nó là không đồng bộFuture fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
- chức năng không đồng bộ. Hàm
6 là một hàm được gắn nhãn bằng từ khóaFuture fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
6Future fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
- chờ đợi. Bạn có thể sử dụng từ khóa
7 để lấy kết quả hoàn thành của biểu thức không đồng bộ. Từ khóaFuture fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
7 chỉ hoạt động trong hàmFuture fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
6Future fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
Luồng thực thi với async và đang chờ
Một hàm
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
6 chạy đồng bộ cho đến từ khóa Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
7 đầu tiên. Điều này có nghĩa là trong thân hàm Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
6, tất cả mã đồng bộ trước từ khóa Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
7 đầu tiên sẽ thực thi ngay lập tứcThí dụ. Thực thi trong các chức năng không đồng bộ
Chạy ví dụ sau để xem quá trình thực thi diễn ra như thế nào trong thân hàm
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
6. Bạn nghĩ đầu ra sẽ là gì?Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
60Sau khi chạy mã trong ví dụ trước, hãy thử đảo ngược dòng 2 và 3
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
61Lưu ý rằng thời gian của đầu ra thay đổi, bây giờ
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
607 xuất hiện sau từ khóa Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
7 đầu tiên trong Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
609Tập thể dục. Thực hành sử dụng async và chờ đợi
Bài tập sau đây là một bài kiểm tra đơn vị không thành công có chứa các đoạn mã đã hoàn thành một phần. Nhiệm vụ của bạn là hoàn thành bài tập bằng cách viết mã để vượt qua bài kiểm tra. Bạn không cần triển khai
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
58Để mô phỏng các hoạt động không đồng bộ, hãy gọi các chức năng sau, được cung cấp cho bạn
FunctionType signatureDescriptionfetchRole[]Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
611Nhận mô tả ngắn về vai trò của người dùng. fetchLoginAmount[]Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
612Lấy số lần người dùng đã đăng nhậpPhần 1. Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
613
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
Thêm mã vào hàm
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
613 để nó thực hiện như sau- Trả về một tương lai hoàn thành với chuỗi sau.
615Future fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
- Ghi chú. Bạn phải sử dụng giá trị thực được trả về bởi
616;Future fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
- Giá trị trả về ví dụ.
617Future fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
- Ghi chú. Bạn phải sử dụng giá trị thực được trả về bởi
- Nhận vai trò người dùng bằng cách gọi hàm được cung cấp
616Future fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
Phần 2. Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
619
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
Triển khai hàm
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
6 Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
619 để nó thực hiện như sau- Trả về chuỗi
622Future fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
- Ghi chú. Bạn phải sử dụng giá trị thực được trả về bởi
623;Future fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
- Giá trị trả về ví dụ từ
619.Future fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
625Future fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
- Ghi chú. Bạn phải sử dụng giá trị thực được trả về bởi
- Nhận số lần đăng nhập bằng cách gọi hàm được cung cấp
623Future fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
62Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
63Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
64Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
65Ghi chú. Nếu mã của bạn vượt qua các bài kiểm tra, bạn có thể bỏ qua các thông báo cấp thông tin
Xử lý lỗi
Để xử lý lỗi trong hàm
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
6, hãy sử dụng try-catchFuture fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
66Trong một hàm
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
6, bạn có thể viết các mệnh đề try-catch giống như cách bạn viết trong mã đồng bộThí dụ. không đồng bộ và chờ đợi với try-catch
Chạy ví dụ sau để xem cách xử lý lỗi từ hàm không đồng bộ. Bạn nghĩ đầu ra sẽ là gì?
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
67Tập thể dục. Thực hành xử lý lỗi
Bài tập sau đây cung cấp cách thực hành xử lý lỗi với mã không đồng bộ, sử dụng cách tiếp cận được mô tả trong phần trước. Để mô phỏng các hoạt động không đồng bộ, mã của bạn sẽ gọi hàm sau, hàm này được cung cấp cho bạn
Chữ ký loại chức năngDescriptionfetchNewUsername[]Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
629Trả lại tên người dùng mới mà bạn có thể sử dụng để thay thế tên người dùng cũSử dụng
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
6 và Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
7 để triển khai hàm Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
632 không đồng bộ thực hiện như sau- Gọi hàm không đồng bộ được cung cấp
633 và trả về kết quả của nóFuture fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
- Giá trị trả về ví dụ từ
632.Future fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
635Future fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
- Giá trị trả về ví dụ từ
- Bắt bất kỳ lỗi nào xảy ra và trả về giá trị chuỗi của lỗi
- Bạn có thể sử dụng phương thức toString[] để xâu chuỗi cả Ngoại lệ và Lỗi
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
68Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
69Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
0Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
1Tập thể dục. Để tất cả chúng cùng nhau
Đã đến lúc thực hành những gì bạn đã học trong một bài tập cuối cùng. Để mô phỏng các hoạt động không đồng bộ, bài tập này cung cấp các chức năng không đồng bộ
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
636 và Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
637Chữ ký loại chức năngDescriptionfetchUsername[]Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
638Trả về tên được liên kết với người dùng hiện tại. logoutUser[]Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
639Thực hiện đăng xuất của người dùng hiện tại và trả lại tên người dùng đã đăng xuấtViết như sau
Phần 1. Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
640
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
- Viết một hàm
640 nhận một đối số duy nhất làFuture fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
98void main[] async { ··· }
- ______3640 trả về đối số
98 của nó trướcvoid main[] async { ··· }
645.Future fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
Ví dụ.
646 trả vềFuture fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
647.Future fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
Phần 2. Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
648
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
- Viết hàm
648 không nhận đối sốFuture fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
- Để lấy tên người dùng,
648 gọi hàm không đồng bộ được cung cấpFuture fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
636Future fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
648 tạo lời chào cho người dùng bằng cách gọiFuture fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
640, chuyển tên người dùng và trả về kết quả.Future fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
Ví dụ. Nếu
636 trả vềFuture fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
655, thìFuture fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
648 trả vềFuture fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
657.Future fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
Phần 3. Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
658
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
- Viết hàm
658 thực hiện các công việc sauFuture fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
- Không có đối số
- Bắt bất kỳ lỗi nào
- Gọi hàm không đồng bộ được cung cấp
637Future fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
- Nếu
637 không thành công,Future fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
658 trả về bất kỳ chuỗi nào bạn muốnFuture fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
- Nếu
637 thành công,Future fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
658 trả về chuỗiFuture fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
665, trong đóFuture fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
666 là giá trị chuỗi được trả về bằng cách gọiFuture fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
637Future fetchUserOrder[] { // Imagine that this function is fetching user info but encounters a bug return Future.delayed[const Duration[seconds: 2], [] => throw Exception['Logout failed: user ID is invalid']]; } void main[] { fetchUserOrder[]; print['Fetching user order...']; }
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
2Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
3Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
4Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
return Future.delayed[const Duration[seconds: 2],
[] => throw Exception['Logout failed: user ID is invalid']];
}
void main[] {
fetchUserOrder[];
print['Fetching user order...'];
}
5Cái gì tiếp theo?
Xin chúc mừng, bạn đã hoàn thành bài học lập trình. Nếu bạn muốn tìm hiểu thêm, sau đây là một số gợi ý về địa điểm tiếp theo
- Chơi với DartPad
- Hãy thử một phòng thí nghiệm lập trình khác
- Tìm hiểu thêm về tương lai và không đồng bộ
- hướng dẫn luồng. Tìm hiểu cách làm việc với chuỗi sự kiện không đồng bộ
- Đồng thời trong Dart Hiểu và tìm hiểu cách triển khai đồng thời trong Dart
- Video phi tiêu từ Google. Xem một hoặc nhiều video về mã hóa không đồng bộ. Hoặc, nếu bạn thích, hãy đọc các bài báo dựa trên các video này. [Bắt đầu với các vòng lặp cô lập và sự kiện. ]
- Nhận SDK phi tiêu
Nếu bạn quan tâm đến việc sử dụng DartPad nhúng, giống như phòng thí nghiệm lập trình này, hãy xem các phương pháp hay nhất để sử dụng DartPad trong hướng dẫn