FlutterFlutter · Chapterبەش 5 · Lectureوانە 5.2
Dart Basics: Variables, Types and Control Flowبنەماکانی Dart: گۆڕاوەکان، جۆرەکان و ڕێڕەوی کۆنترۆڵ
Dart's syntax from main() to loops: comments, var/final/const, int, double, String and bool, string interpolation, sound null safety with ?, !, ??, ??= and late, operators, the course's even/odd program, if/else, switch with Dart 3 patterns, and for, while and do-while loops.
لە main()ـەوە تا : ، var/final/const، int، double، String و bool، ، تەواو بە ?، !، ??، ??= و late، ، جووت/تاکی وانەکە، if/else، switch لەگەڵ patternـەکانی 3 و for، while و do-while.
What you will learnچی فێر دەبیت
- Write a Dart program with
main(), statements and comments. - Declare variables with
var, a type,finalorconst, and choose the right one. - Use int, double, String and bool, string interpolation and type conversion.
- Handle nullable values safely with
?,??,?.,??=, and know the risks of!andlate. - Control the flow with if/else, switch (and switch expressions), loops, break and continue.
- بە
main()، و بنووسیت. - بە
var، جۆرێک،finalیانconstڕابگەیەنیت و گونجاوەکەیان هەڵبژێریت. - int، double، String و bool، و بەکاربهێنیت.
- ئەو دەشێت null بن بە
?،??،?.،??=بە سەلامەتی بەکاربهێنیت و مەترسییەکانی!وlateبزانیت. - ڕێڕەوی بە if/else، switch (و switch expression)، ، break و continue کۆنترۆڵ بکەیت.
main, print and commentsmain، print و لێدوانەکان
A Dart program starts in the function main(). Each instruction inside it is a that ends with a semicolon, and print() writes a line of . Text that the computer should ignore is a : // to the end of the line, /* … */ for several lines, and /// for documentation that tools show as help. The ideas are the same in every language (see Your First Program for Python); here is the Dart way. You can run every example of this lesson with dart run file.dart, or paste it into DartPad.
لە main()ـەوە دەست پێدەکات. هەر فەرمانێک لە ناویدا کە بە فاریزەخاڵ کۆتایی دێت و print() دێڕێک دەنووسێت. ئەو دەقەی کۆمپیوتەر دەبێت پشتگوێی بخات : // تا کۆتایی دێڕەکە، /* … */ بۆ چەند دێڕێک و /// بۆ ڕوونکردنەوە کە ئامرازەکان وەک یارمەتی نیشانی دەدەن. بیرۆکەکان لە هەموو زمانێکدا وەک یەکن (بۆ سەیری یەکەم بەرنامەکەت بکە)؛ ئەمە ڕێگەی . دەتوانیت هەموو نموونەیەکی ئەم وانەیە بە dart run file.dart کار پێبکەیت، یان لە DartPadـدا دایبنێیت.
// A single-line comment: Dart ignores it.
/* A comment that can
cover several lines. */
/// A documentation comment. Tools show it as help text.
void main() {
print('Welcome to CêIT!'); // every statement ends with ;
print(2 + 3);
}Welcome to CêIT! 5
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
A single-line comment.
یەک دێڕی.
- Lineدێڕی 3-4
A comment over several lines.
بۆ چەند دێڕێک.
- Lineدێڕی 6
A documentation comment, shown by the editor as help.
ڕوونکردنەوە، دەستکاریکەرەکە وەک یارمەتی نیشانی دەدات.
- Lineدێڕی 8
A statement ends with
;. A comment can follow it on the same line.بە
;کۆتایی دێت. دەتوانێت لە هەمان دێڕدا بە دوایدا بێت.- Lineدێڕی 9
printcan print any value;2 + 3is worked out first.printدەتوانێت هەر چاپ بکات؛ سەرەتا2 + 3حیساب دەکرێت.
Variablesگۆڕاوەکان
A is a named box for a value. In Dart you create one with var (Dart infers the type) or with a type name such as int, double, String or bool. Either way the box has a fixed type: it can get a new value later, but only of the same type. Names use lowerCamelCase: studentCount, isOpen. The general idea is in Variables and Data Types.
سندووقێکی ناونراوە بۆ . لە بە var دروستی دەکەیت ( جۆرەکە دەدۆزێتەوە) یان بە ناوی جۆرێکی وەک int، double، String یان bool. بە هەر ڕێگەیەک بێت سندووقەکە جۆرێکی جێگیری هەیە: دەتوانێت دواتر نوێ وەربگرێت، بەڵام تەنها لە هەمان جۆر. ناوەکان بە lowerCamelCase دەنووسرێن: studentCount، isOpen. بیرۆکە گشتییەکە لە گۆڕاوەکان و جۆرەکانی دراوەدایە.
void main() {
var city = 'Erbil'; // Dart infers String
int students = 30; // you write the type yourself
double price = 2.5;
bool isOpen = true;
print('$city $students $price $isOpen');
city = 'Duhok'; // a var can get a new value of the same type
students = students + 1;
print('$city $students');
}Erbil 30 2.5 true Duhok 31
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
var: the typeStringcomes from the value.var: جۆریStringلە دێت.- Lineدێڕی 3-5
The type is written first, then the name.
سەرەتا جۆرەکە دەنووسرێت، پاشان ناوەکە.
- Lineدێڕی 6
$cityputs a variable's value inside a string.$cityدەخاتە ناو .- Lineدێڕی 8-9
New values of the same types are fine.
نوێ لە هەمان جۆر دروستن.
Variablesگۆڕاوەکان
final and constfinal و const
Some values should never change. Dart has two keywords for them. final means set once: the value can be calculated while the program runs (like today's date), but after that it cannot be replaced. const means known before the program runs: a the compiler can work out by itself, such as 7 or 3.14159. Every const is also final. Use final or const whenever a value does not need to change — Flutter code uses const a lot, because constant widgets are built only once.
هەندێک نابێت هەرگیز بگۆڕێن. بۆ ئەمانە دوو دەدات. final واتە یەک جار دادەنرێت: دەتوانێت لە کاتی کارکردنی حیساب بکرێت (وەک بەرواری ئەمڕۆ)، بەڵام دوای ئەوە ناگۆڕدرێت. const واتە پێش کارکردنی زانراوە: کە خۆی دەتوانێت بیدۆزێتەوە، وەک 7 یان 3.14159. هەموو constـێک finalـیشە. هەر کاتێک پێویست نەبوو بگۆڕێت final یان const بەکاربهێنە — کۆدی زۆر const بەکار دەهێنێت، چونکە تەنها یەک جار دروست دەکرێن.
void main() {
final name = 'Shilan'; // set once
final today = DateTime.now(); // known only when the program runs
const daysInWeek = 7; // known before the program runs
const pi = 3.14159;
print('$name: a week has $daysInWeek days. Pi is about $pi.');
print(today.year >= 2026);
}Shilan: a week has 7 days. Pi is about 3.14159. true
The second line prints true from 2026 on.
دێڕی دووەم لە 2026ـەوە true چاپ دەکات.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
final: set once.final: یەک جار دادەنرێت.- Lineدێڕی 3
DateTime.now()is known only while running, so it can befinalbut notconst.DateTime.now()تەنها لە کاتی کارکردندا دەزانرێت، بۆیە دەتوانێتfinalبێت بەڵامconstنا.- Lineدێڕی 4-5
const: known before the program runs.const: پێش کارکردنی زانراوە.
void main() {
final name = 'Shilan';
name = 'Hana';
const daysInWeek = 7;
daysInWeek = 8;
const today = DateTime.now();
print('$name $daysInWeek $today');
}final_error.dart:3:3: Error: Can't assign to the final variable 'name'.
name = 'Hana';
^^^^
final_error.dart:6:3: Error: Can't assign to the const variable 'daysInWeek'.
daysInWeek = 8;
^^^^^^^^^^
final_error.dart:8:26: Error: Cannot invoke a non-'const' constructor where a const expression is expected.
Try using a constructor or factory that is 'const'.
const today = DateTime.now();
^^^Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
A
finalvariable cannot get a second value.finalناتوانێت دووەم وەربگرێت.- Lineدێڕی 6
Neither can a
const.constـیش ناتوانێت.- Lineدێڕی 8
DateTime.now()is not known before running, so it cannot beconst.DateTime.now()پێش کارکردن نازانرێت، بۆیە ناتوانێتconstبێت.
| Keyword | Can change?دەگۆڕێت؟ | Value known… کەی دەزانرێت | Exampleنموونە |
|---|---|---|---|
var | Yes (same type)بەڵێ (هەمان جۆر) | any timeهەر کاتێک | var score = 0; |
final | No, set onceنا، یەک جار | while the program runsلە کاتی کارکردنی | final today = DateTime.now(); |
const | Noنا | before the program runsپێش کارکردنی | const daysInWeek = 7; |
Live DartPad coding: var, final and const and their differences, then nullable types with ?, ?. and ?? in Dart 3.
کۆدنووسینی ڕاستەوخۆ لە DartPadـدا: var، final و const و جیاوازییان، پاشان ئەو جۆرانەی دەتوانن بن، بە ?، ?. و ?? ( 3).
Plays 49:02-1:00:24: var, final and const in DartPad, including why const cannot hold DateTime.now().
لە 49:02 تا 1:00:24 پەخش دەبێت: var، final و const لە DartPadـدا، لەگەڵ ئەوەی بۆچی const ناتوانێت DateTime.now() هەڵبگرێت.
Numbers: int and doubleژمارەکان: int و double
Dart has two number types. int holds (whole numbers such as 30 or -4); double holds numbers with a decimal point (, such as 2.5). Both belong to the general type num. The arithmetic operators are the usual ones, with one surprise: / always gives a double, even for 6 / 3 (the result is 2.0). If you want a whole-number result, use ~/. The remainder operator % () tells you what is left over.
دوو جۆر ژمارەی هەیە. int هەڵدەگرێت (ژمارەی بێ کەرتی وەک 30 یان -4)؛ double ژمارەی خاوەن خاڵی دەیی هەڵدەگرێت (، وەک 2.5). هەردووکیان سەر بە جۆری گشتیی numـن. هەمان ئەوانەی ئاسایین، لەگەڵ یەک شتی سەیر: / هەمیشە double دەدات، تەنانەت بۆ 6 / 3 (ئەنجامەکە 2.0ـە). ئەگەر ئەنجامێکی بێ کەرتت دەوێت، ~/ بەکاربهێنە. %، ، پێت دەڵێت چی دەمێنێتەوە.
void main() {
int a = 7;
int b = 2;
print(a + b); // add
print(a - b); // subtract
print(a * b); // multiply
print(a / b); // divide: always a double
print(a ~/ b); // divide, keep only the whole number
print(a % b); // remainder
}9 5 14 3.5 3 1
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 7
7 / 2is3.5: adouble.7 / 2دەکاتە3.5:double.- Lineدێڕی 8
~/divides and keeps only the whole part:3.~/دابەش دەکات و تەنها بەشە تەواوەکە دەهێڵێتەوە:3.- Lineدێڕی 9
7 % 2is1, because .7 % 2دەکاتە1، چونکە .
void main() {
print(-7 ~/ 2); // cuts toward zero
print(-7 % 2); // never negative when the divisor is positive
print((-7).remainder(2)); // keeps the sign of -7
}-3 1 -1
Python's operators are taught in Operators and Expressions; the ideas are the same.
لە کارپێکەرەکان و دەربڕینەکان فێر دەکرێن؛ بیرۆکەکان وەک یەکن.
Numbers: int and doubleژمارەکان: int و double
Converting between typesگۆڕینی جۆر
Text that looks like a number is still text: '19' is a String. turns it into a number with int.parse() or double.parse(); toString() turns a number into text; toStringAsFixed(2) rounds to two decimal places for display. When the text might not be a number (for example something a user typed), use int.tryParse(): it returns null instead of crashing. See also Type conversion in Python.
دەقێک کە لە ژمارە دەچێت هێشتا دەقە: '19' Stringـە. بە int.parse() یان double.parse() دەیکات بە ژمارە؛ toString() ژمارە دەکات بە دەق؛ toStringAsFixed(2) بۆ پیشاندان تا دوو ژمارەی دوای خاڵ نزیکی دەکاتەوە. کاتێک لەوانەیە دەقەکە ژمارە نەبێت (بۆ نموونە شتێک کە نووسیویەتی)، int.tryParse() بەکاربهێنە: لە جیاتی ڕووخان null دەگەڕێنێتەوە. هەروەها سەیری گۆڕینی جۆر لە Pythonـدا بکە.
void main() {
var age = int.parse('19'); // text -> int
var height = double.parse('1.75'); // text -> double
String text = 42.toString(); // number -> text
print(age + 1);
print(height * 2);
print(text.length); // "42" has 2 characters
print(3.14159.toStringAsFixed(2)); // 2 decimal places
print(int.tryParse('abc')); // not a number: null instead of a crash
}20 3.5 2 3.14 null
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
int.parse('19')gives the int 19.int.parse('19')19 دەدات.- Lineدێڕی 4
42.toString()gives the text'42'.42.toString()دەقی'42'دەدات.- Lineدێڕی 8
Rounded to 2 decimal places, as text.
تا دوو ژمارەی دوای خاڵ نزیک کراوەتەوە، وەک دەق.
- Lineدێڕی 9
tryParsegivesnullfor text that is not a number.tryParseبۆ دەقێک کە ژمارە نییەnullدەدات.
Clear Dart examples: list methods (add, insert, remove), maps of key-value pairs, and turning text into numbers with int.parse and double.parse.
نموونەی ڕوونی : (add، insert، remove)، Map بۆ و گۆڕینی بۆ ژمارە بە int.parse و double.parse.
Plays 51:45-56:10: int.parse, double.parse and toString to convert between text and numbers.
لە 51:45 تا 56:10 پەخش دەبێت: int.parse، double.parse و toString بۆ گۆڕین لە نێوان و ژمارەدا.
Stringsزنجیرەنووسەکان
A is text between quotes — single '…' or double "…"; both work the same. The most useful feature is : $name puts a variable's value into the text, and ${…} puts the result of any in it. You can also join strings with +, write several lines between triple quotes '''…''', and stop interpolation with a raw string r'…'. Kurdish text works like any other, because Dart strings are Unicode. Compare Python's f-strings in Input and Output.
دەقێکە لە نێوان — تاک '…' یان جووت "…"؛ هەردووکیان وەک یەک کار دەکەن. بەسوودترین تایبەتمەندی : $name دەخاتە ناو دەقەکەوە و ${…} ئەنجامی هەر دەخاتە ناوی. هەروەها دەتوانیت بە + پێکەوە بلکێنیت، لە نێوان سێ '''…'''ـدا چەند دێڕێک بنووسیت و بە خاو r'…' ڕابگریت. دەقی کوردی وەک هەر دەقێکی تر کار دەکات، چونکە Unicodeـن. بەراوردی بکە لەگەڵ f-stringـەکانی لە نێوخستە و دەرخستە.
void main() {
var name = 'Dilan';
var age = 20;
print('Hello, $name!'); // $name puts the value inside
print('Next year $name will be ${age + 1}.'); // ${...} for an expression
print("It's easy with double quotes.");
print('Tea' + ' and ' + 'bread'); // + joins strings
var poem = '''
Line one
Line two''';
print(poem);
print(r'In a raw string, $name stays as it is.');
print('سڵاو $name'); // Kurdish text works too
print(name.toUpperCase());
print(name.length);
}Hello, Dilan! Next year Dilan will be 21. It's easy with double quotes. Tea and bread Line one Line two In a raw string, $name stays as it is. سڵاو Dilan DILAN 5
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 4
$name→ the value ofname.$name←name.- Lineدێڕی 5
${age + 1}→ the result of the expression.${age + 1}← ئەنجامی .- Lineدێڕی 6
Double quotes make an apostrophe easy.
جووت نووسینی
'ئاسان دەکات.- Lineدێڕی 8-10
Triple quotes keep the line breaks.
سێ دێڕە نوێکان دەپارێزێت.
- Lineدێڕی 12
r'…':$nameis printed as it is.r'…':$nameوەک خۆی چاپ دەکرێت.- Lineدێڕی 14-15
Strings have methods and properties, such as
toUpperCase()andlength.و تایبەتمەندییان هەیە، وەک
toUpperCase()وlength.
Stringsزنجیرەنووسەکان
Booleansبوولیانەکان
A bool () has only two values, true and false. Comparisons give booleans. Dart is strict here: a condition must be a real bool. Some languages treat 0, an empty text or null as false (“truthy” and “falsy” values — see Python's truthiness); Dart does not, so you write the check yourself: count == 0, name.isEmpty, value == null.
bool () تەنها دوو هەیە، true و false. بەراوردکردنەکان دەدەن. لێرەدا توندە: دەبێت boolـێکی ڕاستەقینە بێت. هەندێک زمان 0، دەقێکی بەتاڵ یان null وەک false دادەنێن ( «truthy» و «falsy» — سەیری truthinessـی Python بکە)؛ وا ناکات، بۆیە پشکنینەکە خۆت دەنووسیت: count == 0، name.isEmpty، value == null.
void main() {
var count = 0;
var noMessages = count == 0; // a comparison gives a bool
print(noMessages);
var name = '';
if (name.isEmpty) {
print('Please enter your name.');
}
}true Please enter your name.
int مەرج نییەbool_error.dartDARTType in a terminalلە تێرمیناڵدا بینووسەvoid main() {
var count = 0;
if (count) {
print('You have messages.');
}
}bool_error.dart:3:7: Error: A value of type 'int' can't be assigned to a variable of type 'bool'.
if (count) {
^Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
Dart refuses an
intwhere aboolis needed. Writeif (count > 0).intلە شوێنێک کەboolـی پێویستە ڕەت دەکاتەوە. بنووسەif (count > 0).
Null safetyپاراستن لە بێنرخ
null means “no value”. In many languages any variable can secretly be null, and the program crashes the moment you use it — one of the most common bugs in apps. Dart's sound stops this before the program runs: a normal type (int, String) can never hold null. If a value may be missing, add a question mark to its type: int?, String?. Such a nullable variable starts as null, and Dart will not let you use it as if it were sure to have a value.
null () واتە «هیچ ». لە زۆر زماندا هەر دەتوانێت بە نهێنی null بێت و لەو ساتەی بەکاری دەهێنیت دەڕووخێت — یەکێک لە باوترین . تەواوی ئەمە پێش کارکردنی ڕادەگرێت: جۆرێکی ئاسایی (int، String) هەرگیز ناتوانێت null هەڵبگرێت. ئەگەر لەوانەیە نەبێت، نیشانەی پرسیار بخە سەر جۆرەکەی: int?، String?. وا بە null دەست پێدەکات و ڕێگە نادات وەک ئەوەی دڵنیا بێت هەیە بەکاری بهێنیت.
void main() {
int age = null;
String? nickname;
print(nickname.length);
print(age);
}null_error.dart:2:13: Error: A value of type 'Null' can't be assigned to a variable of type 'int'.
int age = null;
^
null_error.dart:4:18: Error: Property 'length' cannot be accessed on 'String?' because it is potentially null.
Try accessing using ?. instead.
print(nickname.length);
^^^^^^Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
intcannot holdnull.intناتوانێتnullهەڵبگرێت.- Lineدێڕی 3
String?may holdnull; here it starts asnull.String?دەتوانێتnullهەڵبگرێت؛ لێرەدا بەnullدەست پێدەکات.- Lineدێڕی 4
Using
.lengthon aString?is refused: it might benull. The message even suggests?..بەکارهێنانی
.lengthلەسەرString?ڕەت دەکرێتەوە: لەوانەیەnullبێت. پەیامەکە تەنانەت?.پێشنیار دەکات.


null ڕادەگرێت پێش ئەوەی بڕووخێنێت.CêITLive DartPad coding: var, final and const and their differences, then nullable types with ?, ?. and ?? in Dart 3.
کۆدنووسینی ڕاستەوخۆ لە DartPadـدا: var، final و const و جیاوازییان، پاشان ئەو جۆرانەی دەتوانن بن، بە ?، ?. و ?? ( 3).
Plays 1:00:24-1:12:00: nullable types (String?), the error when null goes into int, ?. and ??.
لە 1:00:24 تا 1:12:00 پەخش دەبێت: ئەو جۆرانەی دەتوانن بن (String?)، ئەو دروست دەبێت کاتێک دەخرێتە ناو int، پاشان ?. و ??.
Null safetyپاراستن لە بێنرخ
Working with nullable valuesکارکردن لەگەڵ ئەو نرخانەی دەشێت null بن
Dart gives you short operators for nullable values:
چەند کورتت بۆ ئەو دەداتێ کە دەشێت null بن:
| Operator | Meaningواتا | Exampleنموونە |
|---|---|---|
?? | If the left side is null, use the right sideئەگەر لای چەپ null بوو، لای ڕاست بەکاربهێنە | nickname ?? 'guest' |
??= | Assign only if the variable is nullتەنها ئەگەر null بوو دابنێ | nickname ??= 'Kawa'; |
?. | Use a member only if not null; otherwise the result is nullتەنها ئەگەر null نەبوو بەکاری بهێنە؛ ئەگینا ئەنجامەکە nullـە | nickname?.length |
! | “Trust me, it is not null” — crashes if it is«متمانەم پێ بکە، null نییە» — ئەگەر بوو دەڕووخێت | nickname!.length |
You can also simply check for null: inside if (score != null) { … } Dart knows that score has a value and lets you use it as an int. This is called type promotion.
هەروەها دەتوانیت بە سادەیی null بپشکنیت: لە ناو if (score != null) { … }ـدا دەزانێت کە score هەیە و ڕێگەت دەدات وەک int بەکاری بهێنیت. ئەمە پێی دەوترێت بەرزکردنەوەی جۆر.
void main() {
String? nickname; // may be null; it starts as null
print(nickname);
print(nickname ?? 'no nickname'); // ?? gives a default
print(nickname?.length); // ?. stops at null: no crash
nickname ??= 'Kawa'; // give it a value only if it is null
print(nickname.length); // Dart now knows it is not null
int? score = int.tryParse('85');
if (score != null) {
print(score + 5); // inside the check, score is an int
}
}null no nickname null 4 90
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 4
??gives a default instead ofnull.??دەدات لە جیاتیnull.- Lineدێڕی 5
?.stops atnulland givesnull.?.لەnullـدا دەوەستێت وnullدەدات.- Lineدێڕی 7-8
After
??=, Dart knowsnicknameis not null, so.lengthis allowed.دوای
??=، دەزانێتnicknamenull نییە، بۆیە.lengthڕێگەپێدراوە.- Lineدێڕی 10-13
int.tryParsereturnsint?. Inside theif,scoreis a normalint.int.tryParseint?دەگەڕێنێتەوە. لە ناوifـداscoreintـێکی ئاساییە.
void main() {
String? city; // nobody gave it a value
print('Before');
print(city!.length); // "trust me, it is not null" - but it is
print('After');
}Before Unhandled exception: Null check operator used on a null value #0 main (bang_crash.dart:4:13)
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 4
!switches the check off for this one use. The value isnull, so the program stops here andAfteris never printed.!پشکنینەکە بۆ ئەم یەک بەکارهێنانە دەکوژێنێتەوە.nullـە، بۆیە لێرەدا دەوەستێت وAfterهەرگیز چاپ ناکرێت.
Null safetyپاراستن لە بێنرخ
late variablesگۆڕاوەکانی late
Sometimes a variable cannot get its value where it is declared, but you know it will be set before anyone reads it. Mark it late. Dart then trusts you and checks only at runtime: reading a late variable before it has a value throws a LateInitializationError. In Flutter you will meet late for values that are prepared in initState() (see the lifecycle of a State object).
هەندێک جار ناتوانێت لە شوێنی ڕاگەیاندنیدا وەربگرێت، بەڵام دەزانیت پێش ئەوەی کەس بیخوێنێتەوە دادەنرێت. نیشانەی lateـی لێ بدە. ئینجا متمانەت پێ دەکات و تەنها لە دەپشکنێت: خوێندنەوەی late پێش ئەوەی هەبێت LateInitializationError دروست دەکات. لە late بۆ ئەو دەبینیت کە لە initState()ـدا ئامادە دەکرێن (سەیری سووڕی ژیانی ئۆبجێکتی دۆخ بکە).
late String greeting; // a value comes later
void main() {
greeting = 'Good morning';
print(greeting);
}Good morning
late String message;
void main() {
print('Start');
print(message); // read before it was set
}Start Unhandled exception: LateInitializationError: Field 'message' has not been initialized. #0 message (late_crash.dart) #1 main (late_crash.dart:5:9)
More operatorsکارپێکەرەکانی زیاتر
Besides arithmetic, you will use these every day: ++ and -- add or take away 1; +=, -=, *= change a variable using its own value; (==, !=, >, <, >=, <=) give a bool; combine conditions — && (and), || (or), ! (not); the condition ? a : b chooses one of two values; and is tests a type. The ideas are explained for every language in Operators and Expressions and Logical Operators.
جگە لە ، ئەم هەموو ڕۆژێک بەکار دەهێنیت: ++ و -- 1 زیاد یان کەم دەکەن؛ +=، -=، *= بە خۆی دەگۆڕن؛ (==، !=، >، <، >=، <=) bool دەدەن؛ پێکەوە دەبەستن — && (و)، || (یان)، ! (نا)؛ condition ? a : b یەکێک لە دوو هەڵدەبژێرێت؛ و is جۆرێک دەپشکنێت. بیرۆکەکان بۆ هەموو زمانێک لە کارپێکەرەکان و دەربڕینەکان و کارپێکەرە لۆجیکییەکان ڕوون کراونەتەوە.
void main() {
var score = 10;
score++; // score = score + 1
score += 5; // score = score + 5
score -= 2;
print(score);
print(score == 14);
print(score != 14);
print(score > 10 && score < 20); // and
print(score < 0 || score > 100); // or
print(!(score > 10)); // not
var result = score >= 50 ? 'pass' : 'fail'; // condition ? a : b
print(result);
print(score is int); // type test
}14 true false true false false fail true
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3-5
10 → 11 → 16 → 14.
10 دەبێت بە 11، پاشان 16، پاشان 14.
- Lineدێڕی 8-9
==asks “equal?”,!=asks “not equal?”.==دەپرسێت «یەکسانە؟»،!=دەپرسێت «یەکسان نییە؟».- Lineدێڕی 10-12
&&needs both sides true;||needs one;!turns true into false.&&پێویستی بە ڕاستبوونی هەردوو لایە؛||پێویستی بە یەکیانە؛!true دەکات بە false.- Lineدێڕی 14
Ternary:
score >= 50is false, so the result is'fail'.:
score >= 50falseـە، بۆیە ئەنجامەکە'fail'ـە.- Lineدێڕی 16
ischecks the type.isجۆرەکە دەپشکنێت.
More operatorsکارپێکەرەکانی زیاتر
The course example: even or oddنموونەی وانەکە: جووت یان تاک
The course's first Dart program decides whether a number is even or odd. The idea: a number is even when dividing it by 2 leaves no remainder, so number % 2 == 0. The flowchart under the code draws the same decision; flowchart shapes are explained in Flowchart symbols.
یەکەم وانەکە بڕیار دەدات ژمارەیەک جووتە یان تاک. بیرۆکەکە: ژمارەیەک جووتە کاتێک دابەشکردنی بەسەر 2دا هیچ نامێنێت، کەواتە number % 2 == 0. ئەو لە ژێر کۆدەکەدایە هەمان بڕیار دەکێشێت؛ شێوەکانی لە هێماکانی هێڵکاریی ڕەوت ڕوون کراونەتەوە.
void main() {
var number = 12;
if (number % 2 == 0) {
print("Number is Even.");
} else {
print("Number is Odd.");
}
}Number is Even.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
The course called this variable
num; we usenumber(see the note below).وانەکە ناوی ئەم نابوو
num؛ ئێمەnumberبەکار دەهێنین (سەیری تێبینییەکەی خوارەوە بکە).- Lineدێڕی 3
%gives the remainder;==compares it with 0.%دەدات؛==بەراوردی دەکات لەگەڵ 0.
if, else if and elseif، else if و else
Choosing and repeating are two of the three building blocks of every algorithm; this screen and the next ones show how Dart writes them. An runs a only when its is true. Add else if for more conditions and else for “everything else”. Dart checks them from the top and runs only the first block whose condition is true. The condition goes in parentheses and must be a bool; the blocks go in braces. (Python uses elif and a colon for the same thing — see Making Decisions.)
هەڵبژاردن و دووبارەکردنەوە دوو لە سێ بەردی بناغەی هەموو ؛ ئەم شاشەیە و شاشەکانی دواتر نیشان دەدەن چۆن دەیاننووسێت. تەنها کاتێک جێبەجێ دەکات کە ڕاست بێت. else if بۆ زیاتر و else بۆ «هەموو شتەکانی تر» زیاد بکە. لە سەرەوە دەیانپشکنێت و تەنها یەکەم جێبەجێ دەکات کە ڕاستە. لە ناو کەوانەدا دەنووسرێت و دەبێت bool بێت؛ لە ناو کەوانەی لوولیدان. ( بۆ هەمان کار elif و دوو خاڵ بەکار دەهێنێت — سەیری بڕیاردان بکە.)
void main() {
var mark = 72;
if (mark >= 90) {
print('Excellent');
} else if (mark >= 70) {
print('Very good');
} else if (mark >= 50) {
print('Pass');
} else {
print('Try again');
}
}Very good
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
72 >= 90 is false…
72 >= 90 falseـە…
- Lineدێڕی 5
…72 >= 70 is true, so this block runs and the rest are skipped.
…72 >= 70 trueـە، بۆیە ئەم کار دەکات و ئەوانی تر تێدەپەڕێنرێن.
switchڕستەی switch
A compares one value with several cases. In Dart 3 a case does not fall through into the next one, so no break is needed; empty cases placed together share one body; and default (or _) catches everything else. Dart 3 also has a switch expression, which produces a value: each case is pattern => result, separated by commas.
یەک لەگەڵ چەند حاڵەتێک بەراورد دەکات. لە 3دا حاڵەتێک ناچێتە ناو حاڵەتی دواتر، بۆیە پێویستی بە break نییە؛ حاڵەتە بەتاڵەکان کە پێکەوە دانراون یەک ناوەڕۆک هاوبەش دەکەن؛ و default (یان _) هەموو شتەکانی تر دەگرێت. 3 switch expressionـیشی هەیە کە دروست دەکات: هەر حاڵەتێک pattern => resultـە و بە فاریزە جیا دەکرێنەوە.
void main() {
var day = 'Friday';
switch (day) {
case 'Friday':
case 'Saturday':
print('Weekend');
case 'Thursday':
print('Almost the weekend');
default:
print('A working day');
}
var mark = 85;
var grade = switch (mark) {
>= 90 => 'A',
>= 80 => 'B',
>= 70 => 'C',
>= 50 => 'D',
_ => 'F',
};
print(grade);
}Weekend B
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 4-6
Two cases share one body. No
breakis needed.دوو حاڵەت یەک ناوەڕۆک هاوبەش دەکەن. پێویست بە
breakناکات.- Lineدێڕی 9
defaultruns when no case matches.defaultکاتێک هیچ حاڵەتێک نەگونجا کار دەکات.- Lineدێڕی 14-20
The switch expression gives a value to
grade.>= 90is a relational pattern;_matches everything else. The first matching case wins.switch expression دەدات بە
grade.>= 90patternـێکی بەراوردکردنە؛_هەموو شتەکانی تر دەگرێت. یەکەم حاڵەتی گونجاو دەباتەوە.
switchڕستەی switch
Dart 3 patterns, brieflypattern لە Dart 3دا، بە کورتی
The case part is a pattern: a shape the value is matched against. Beyond plain values, three kinds are useful from the start: a || b matches either value; relational patterns such as > 40 compare; and a guard when … adds an extra condition. Python 3.10 has a similar match statement (see it here).
بەشی case patternـێکە: شێوەیەک کە لەگەڵیدا بەراورد دەکرێت. جگە لە سادە، سێ جۆریان لە سەرەتاوە بەسوودن: a || b هەریەک لە دوو دەگرێت؛ patternـی بەراوردکردن وەک > 40 بەراورد دەکەن؛ و پاسەوانێک when … زیادە دادەنێت. 3.10 ڕستەی matchـی هاوشێوەی هەیە (لێرە ببینە).
void main() {
for (var month in [1, 4, 7, 11]) {
var season = switch (month) {
12 || 1 || 2 => 'winter',
3 || 4 || 5 => 'spring',
6 || 7 || 8 => 'summer',
_ => 'autumn',
};
print('Month $month: $season');
}
var temperature = 41;
switch (temperature) {
case > 40 when temperature < 50:
print('Very hot: stay inside at noon.');
case > 25:
print('Warm');
default:
print('Cool');
}
}Month 1: winter Month 4: spring Month 7: summer Month 11: autumn Very hot: stay inside at noon.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 4-7
12 || 1 || 2matches any of the three months.12 || 1 || 2هەریەک لە سێ مانگەکە دەگرێت.- Lineدێڕی 14
case > 40 when temperature < 50:— the pattern must match and the guard must be true.case > 40 when temperature < 50:— دەبێت patternـەکە بگونجێت و پاسەوانەکەش ڕاست بێت.
Loops: for, for-in, while, do-whileخولەکان: for، for-in، while، do-while
A repeats a block. Dart has four kinds. The classic has three parts — start, condition, step — and is best for counting. for-in walks through every item of a list. The repeats while a condition is true, checking it before each round. The checks after each round, so its body always runs at least once — Python has no such loop (see why). The ideas are taught step by step in The for Loop and The while Loop.
دووبارە دەکاتەوە. چوار جۆری هەیە. کلاسیک سێ بەشی هەیە — دەستپێک، ، هەنگاو — و بۆ ژماردن باشترینە. for-in بەسەر هەموو دەڕوات. تا ڕاست بێت دووبارە دەکاتەوە و پێش هەر دەیپشکنێت. دوای هەر دەپشکنێت، بۆیە ناوەڕۆکەکەی هەمیشە لانیکەم یەک جار کار دەکات — وای نییە (بۆچی). بیرۆکەکان هەنگاو بە هەنگاو لە خولی for و خولی while فێر دەکرێن.
void main() {
for (var i = 0; i < 5; i++) {
print('i = $i');
}
var cities = ['Erbil', 'Duhok', 'Halabja'];
for (final city in cities) {
print(city);
}
var glasses = 3;
while (glasses > 0) {
print('Glasses of tea left: $glasses');
glasses--;
}
var tries = 0;
do {
tries++;
print('Try $tries');
} while (tries < 2);
}i = 0 i = 1 i = 2 i = 3 i = 4 Erbil Duhok Halabja Glasses of tea left: 3 Glasses of tea left: 2 Glasses of tea left: 1 Try 1 Try 2
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
Start
i = 0; repeat whilei < 5; after each roundi++.دەستپێک
i = 0؛ تاi < 5دووبارە بکەرەوە؛ دوای هەرi++.- Lineدێڕی 7-9
for-in:
citytakes each item in turn.finalbecause it is not changed inside.for-in:
cityبە نۆبە هەر وەردەگرێت.finalـە چونکە لە ناوەوە ناگۆڕدرێت.- Lineدێڕی 12-15
while: checked before each round;
glasses--moves towards the end.while: پێش هەر دەپشکنرێت؛
glasses--بەرەو کۆتایی دەبات.- Lineدێڕی 18-21
do-while: the body runs first, then the condition is checked.
do-while: سەرەتا ناوەڕۆکەکە کار دەکات، پاشان دەپشکنرێت.
Loops: for, for-in, while, do-whileخولەکان: for، for-in، while، do-while
break and continuebreak و continue
Inside a loop, a skips the rest of this round and goes on with the next one; a leaves the loop completely. (The same in Python.)
لە ناو ، پاشماوەی ئەم تێدەپەڕێنێت و بەردەوام دەبێت لەگەڵ دواتر؛ بە تەواوی لە دەردەچێت. (هەمان شت لە Pythonـدا.)
void main() {
for (var n = 1; n <= 10; n++) {
if (n == 3) continue; // skip 3
if (n == 6) break; // stop at 6
print(n);
}
}1 2 4 5
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
continue: 3 is not printed, but the loop goes on.continue: 3 چاپ ناکرێت، بەڵام بەردەوام دەبێت.- Lineدێڕی 4
break: at 6 the loop ends, so 6–10 are never printed.break: لە 6دا کۆتایی دێت، بۆیە 6–10 هەرگیز چاپ ناکرێن.
Common mistakesهەڵە باوەکان
| Mistakeهەڵە | Fixچاککردنەوە |
|---|---|
if (x = 5) | = assigns, == compares: if (x == 5). Dart refuses the first, because an assignment is not a bool.= دادەنێت، == بەراورد دەکات: if (x == 5). یەکەمیان ڕەت دەکاتەوە، چونکە bool نییە. |
A missing ;;ـی لەبیرکراو | Every statement ends with a semicolon; the compiler shows the line.هەموو بە فاریزەخاڵ کۆتایی دێت؛ دێڕەکە نیشان دەدات. |
int total = null; | Use int? total; if it may be empty, or give it a real value: int total = 0;.ئەگەر لەوانەیە بەتاڵ بێت int? total; بەکاربهێنە، یان ڕاستەقینەی پێ بدە: int total = 0;. |
nickname!.length | ! crashes when the value is null. Prefer nickname?.length ?? 0 or an if check.! کاتێک null بێت دەڕووخێنێت. nickname?.length ?? 0 یان پشکنینی if باشترە. |
if (count) { … } | A condition must be a bool: if (count > 0). دەبێت bool بێت: if (count > 0). |
int half = 7 / 2; | / gives a double. Use 7 ~/ 2 for an int, or double half = 7 / 2;./ double دەدات. بۆ int 7 ~/ 2 بەکاربهێنە، یان double half = 7 / 2;. |
'Total: ' + 5 | You cannot add an int to a String. Write 'Total: $total' or 'Total: ' + total.toString().ناتوانیت int بخەیتە سەر String. بنووسە 'Total: $total' یان 'Total: ' + total.toString(). |
'$n * n' | Inside a string, an expression needs braces: '${n * n}'.لە ناو ، پێویستی بە کەوانەی لوولی هەیە: '${n * n}'. |
Expecting fall-through in switchچاوەڕوانکردنی چوونە خوارەوە لە switchـدا | Dart 3 cases end by themselves; put empty cases together to share a body.حاڵەتەکانی 3 خۆیان کۆتایی دێن؛ حاڵەتە بەتاڵەکان پێکەوە دابنێ بۆ هاوبەشکردنی ناوەڕۆک. |
A while loop that never ends کە هەرگیز کۆتایی نایەت | Make sure something inside the loop moves the condition towards false (glasses--).دڵنیا بە شتێک لە ناو بەرەو false دەبات (glasses--). |
Exercisesڕاهێنانەکان
Write a program with const celsius = 30; that calculates the temperature in Fahrenheit () and prints 30 °C = 86 °F. Should fahrenheit be var, final or const?
بنووسە بە const celsius = 30; کە پلەی گەرمی بە فەهرەنهایت () حیساب بکات و 30 °C = 86 °F چاپ بکات. ئایا fahrenheit دەبێت var، final یان const بێت؟
Show answerوەڵامەکە پیشان بدە
void main() {
const celsius = 30;
final fahrenheit = celsius * 9 / 5 + 32;
print('$celsius °C = $fahrenheit °F');
print('$celsius °C = ${fahrenheit.toStringAsFixed(0)} °F');
}30 °C = 86.0 °F 30 °C = 86 °F
final (or even const, because celsius is a constant) — the value never changes. / gives a double, so the first line shows 86.0; toStringAsFixed(0) shows 86.
final (یان تەنانەت const، چونکە celsius ) — هەرگیز ناگۆڕێت. / double دەدات، بۆیە دێڕی یەکەم 86.0 نیشان دەدات؛ toStringAsFixed(0) 86 نیشان دەدات.
Change the even/odd program to test 7. Then add a second check that says whether the number can be divided by 3, and if not, what remains.
جووت/تاک بگۆڕە بۆ ئەوەی 7 تاقی بکاتەوە. پاشان پشکنینێکی دووەم زیاد بکە کە بڵێت ئایا ژمارەکە بەسەر 3دا دابەش دەبێت و ئەگەر نا، چی دەمێنێتەوە.
Show answerوەڵامەکە پیشان بدە
void main() {
var number = 7;
if (number % 2 == 0) {
print("Number is Even.");
} else {
print("Number is Odd.");
}
if (number % 3 == 0) {
print('$number can be divided by 3.');
} else {
print('$number leaves ${number % 3} when divided by 3.');
}
}Number is Odd. 7 leaves 1 when divided by 3.
Declare String? city;. Print the length of the city name, or 0 if there is none — in one line, without !. Then give city the value 'Sulaymaniyah' and print its length again.
String? city; ڕابگەیەنە. درێژیی ناوی شارەکە چاپ بکە، یان 0 ئەگەر نەبوو — لە یەک دێڕدا، بەبێ !. پاشان 'Sulaymaniyah' بدە بە city و دووبارە درێژییەکەی چاپ بکە.
Show answerوەڵامەکە پیشان بدە
void main() {
String? city; // the user has not chosen a city yet
print(city?.length ?? 0);
city = 'Sulaymaniyah';
print(city.length);
}0 12
After the assignment Dart knows city is not null, so city.length needs no ?..
دوای دانانی ، دەزانێت city null نییە، بۆیە city.length پێویستی بە ?. نییە.
Turn the if … else if mark example into a switch expression and test it with 95, 64 and 49.
نموونەی نمرەی if … else if بگۆڕە بۆ switch expression و بە 95، 64 و 49 تاقی بکەرەوە.
Hintئاماژە
Use relational patterns such as >= 70 and _ for the rest.
patternـی بەراوردکردنی وەک >= 70 و _ بۆ ئەوانی تر بەکاربهێنە.
Show answerوەڵامەکە پیشان بدە
void main() {
for (final mark in [95, 64, 49]) {
var grade = switch (mark) {
>= 90 && <= 100 => 'Excellent',
>= 70 => 'Very good',
>= 50 => 'Pass',
_ => 'Try again',
};
print('$mark: $grade');
}
}95: Excellent 64: Pass 49: Try again
Print the multiplication table of 7 (from 7 x 1 to 7 x 10) with a for loop. Then add up the numbers from 1 to 100 with a while loop.
خشتەی لێکدانی 7 (لە 7 x 1 تا 7 x 10) بە چاپ بکە. پاشان ژمارەکانی 1 تا 100 بە کۆ بکەرەوە.
Show answerوەڵامەکە پیشان بدە
void main() {
for (var i = 1; i <= 10; i++) {
print('7 x $i = ${7 * i}');
}
var sum = 0;
var n = 1;
while (n <= 100) {
sum += n;
n++;
}
print('1 + 2 + ... + 100 = $sum');
}7 x 1 = 7 7 x 2 = 14 7 x 3 = 21 7 x 4 = 28 7 x 5 = 35 7 x 6 = 42 7 x 7 = 49 7 x 8 = 56 7 x 9 = 63 7 x 10 = 70 1 + 2 + ... + 100 = 5050
This program does not compile. Find the three mistakes and fix them.
ئەم کۆمپایل ناکرێت. سێ هەڵەکە بدۆزەرەوە و چاکیان بکەرەوە.
void main() {
int count = null;
if (count) {
print('Total: ' + count);
}
}Show answerوەڵامەکە پیشان بدە
void main() {
int count = null;
if (count) {
print('Total: ' + count);
}
}bugs.dart:2:15: Error: A value of type 'Null' can't be assigned to a variable of type 'int'.
int count = null;
^
bugs.dart:3:7: Error: A value of type 'int' can't be assigned to a variable of type 'bool'.
if (count) {
^
bugs.dart:4:23: Error: A value of type 'int' can't be assigned to a variable of type 'String'.
print('Total: ' + count);
^void main() {
int count = 0; // a real starting value
if (count > 0) {
// a real bool condition
print('Total: $count'); // interpolation turns the int into text
} else {
print('Nothing yet.');
}
}Nothing yet.
Summaryپوختە
- A program starts in
main(); statements end with;; comments are//,/* */and///. varinfers the type;finalis set once;constis known before running.int,double,String,bool./gives a double,~/an int,%the remainder. Convert withparse,tryParse,toString.- Strings:
$nameand${expression}. Conditions must be realbools. - Null safety: only
Type?may be null; use??,?.,??=or anifcheck;!andlatemove the check to runtime. if / else if / else;switchwith no fall-through, switch expressions and patterns;for, for-in,while,do-while,break,continue.
- لە
main()ـەوە دەست پێدەکات؛ بە;کۆتایی دێن؛//،/* */و///ـن. varجۆرەکە دەدۆزێتەوە؛finalیەک جار دادەنرێت؛constپێش کارکردن زانراوە.int،double،String،bool./double دەدات،~/int،%پاشماوە. بەparse،tryParse،toStringبیگۆڕە.- :
$nameو${expression}. دەبێتboolـی ڕاستەقینە بن. - : تەنها
Type?دەتوانێت null بێت؛??،?.،??=یان پشکنینیifبەکاربهێنە؛!وlateپشکنینەکە دەگوازنەوە بۆ . if / else if / else؛switchبەبێ چوونە خوارەوە، switch expression و pattern؛for، for-in،while،do-while،break،continue.
What is the difference between final and const?جیاوازیی نێوان final و const چییە؟
Both cannot change. final may get its value while the program runs; const must be known before.
هیچیان ناگۆڕێن. final دەتوانێت لە کاتی کارکردندا وەربگرێت؛ const دەبێت پێشتر زانراو بێت.
What does a ?? b give?a ?? b چی دەدات؟
a if it is not null, otherwise b.
a ئەگەر null نەبێت، ئەگینا b.
Why does if (count) not compile in Dart?بۆچی if (count) لە Dartـدا کۆمپایل ناکرێت؟
A condition must be a bool; Dart has no truthy values. Write if (count > 0).
دەبێت bool بێت؛ truthyـی نییە. بنووسە if (count > 0).
Videoڤیدیۆ
Watch a videoسەیری ڤیدیۆیەک بکە
A video that viewers liked a lot and praised in their comments, chosen for this lesson.ڤیدیۆیەک کە بینەران زۆریان پێ خۆش بووە و لە کۆمێنتەکانیاندا ستایشیان کردووە، بۆ ئەم وانەیە هەڵبژێردراوە.
Mitch Koko's calm beginner course in Dart and Flutter: variables, operators, if/else, switch, loops, functions, collections, a first screen and a counter.
کۆرسە ئارامەکەی Mitch Koko بۆ سەرەتاییەکان لە و : ، ، if/else، switch، ، ، List/Set/Map، یەکەم شاشە و .
Plays 3:31-19:21: variables, math, comparison and logical operators, if/else, switch, for and while loops.
لە 3:31 تا 19:21 پەخش دەبێت: ؛ ، و ؛ if/else و switch؛ و .
Lesson complete!وانەکە تەواو بوو!
Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.
Key termsزاراوە سەرەکییەکان
Dart Functions and Collectionsکردارەکان و کۆکراوەکانی Dart
Related lecturesوانە پەیوەندیدارەکان
References and further readingسەرچاوەکان و خوێندنەوەی زیاتر
This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.