<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by PriyaRanjan Prakash on Medium]]></title>
        <description><![CDATA[Stories by PriyaRanjan Prakash on Medium]]></description>
        <link>https://medium.com/@pryrnjn?source=rss-7e16cfd87c02------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*SAjy4QUf4JVhtMMj.</url>
            <title>Stories by PriyaRanjan Prakash on Medium</title>
            <link>https://medium.com/@pryrnjn?source=rss-7e16cfd87c02------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 12:22:55 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@pryrnjn/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Understanding Dart’s null safety and ‘?’]]></title>
            <link>https://pryrnjn.medium.com/understanding-darts-null-safety-and-0666a8a68fcc?source=rss-7e16cfd87c02------2</link>
            <guid isPermaLink="false">https://medium.com/p/0666a8a68fcc</guid>
            <category><![CDATA[question-mark]]></category>
            <category><![CDATA[dart]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[null-safety]]></category>
            <dc:creator><![CDATA[PriyaRanjan Prakash]]></dc:creator>
            <pubDate>Fri, 29 Nov 2024 22:12:33 GMT</pubDate>
            <atom:updated>2024-11-29T22:31:22.119Z</atom:updated>
            <content:encoded><![CDATA[<h3>Premise</h3><p>Very recently, I became curious about the Flutter framework and Dart language. The primary reason was that I wanted to write an app with the least effort for Android, iOS, and Web.</p><p>I got introduced to Dart’s standout feature — null safety and mystery of `?`, however, I have seen ‘?’ in Typescript but never fully understood the idea. After learning in the context of Dart, I thought of putting the whole concept concisely, like a crash course, but good enough to get started. So, enough premise, let’s get to the point.</p><h3>The body</h3><p>Dart’s <strong>null safety</strong> is a key feature introduced to help avoid null errors (a common source of bugs). It ensures that variables cannot contain null unless explicitly allowed, enabling you to write safer and more predictable code.</p><p><strong>1. Basics of Null Safety</strong></p><p>• By default, all variables in Dart are <strong>non-nullable</strong>.</p><p>• You must explicitly declare a variable as nullable using a ‘?’.</p><p><strong>Examples:</strong></p><pre>// Non-nullable variable<br>int number = 10; // Cannot be null<br>// Nullable variable<br>int? nullableNumber = null; // Can store null</pre><p><strong>2. Working With Nullable Variables</strong></p><p>When a variable is nullable, you must handle potential null values explicitly to avoid runtime errors.</p><p><strong>Example:</strong></p><pre>int? age; // Nullable variable<br>if (age != null) {<br>  print(&quot;Age is $age&quot;);<br>} else {<br>  print(&quot;Age is not set&quot;);<br>}</pre><p><strong>3. Null Assertion (!)</strong></p><p>The ! operator tells Dart that you are <strong>certain</strong> a nullable value is not null. Use it cautiously, as it can cause runtime exceptions if the value is actually null. Overusing ! is considered bad practice as it negates the advantages of null safety</p><p><strong>Example:</strong></p><pre>int? nullableValue = 5;<br>int nonNullableValue = nullableValue!; // Forces conversion to non-nullable<br>print(nonNullableValue);</pre><p><strong>4. Default Values Using the ?? Operator</strong></p><p>The ?? operator provides a fallback value if a nullable variable is null.</p><p><strong>Example:</strong></p><pre>int? nullableValue;<br>int nonNullableValue = nullableValue ?? 0; // Defaults to 0 if null<br>print(nonNullableValue); // Output: 0</pre><p><strong>5. Late Variables</strong></p><p>If you want a non-nullable variable but cannot initialize it immediately, use the late keyword. This tells Dart that the variable will be assigned before it’s accessed.</p><p><strong>Example:</strong></p><pre>late String username;<br>username = &quot;Dart Dev&quot;; // Must be initialized before use<br>print(username);</pre><p><strong>6. Null Safety in Functions</strong></p><p><strong>Nullable Parameters:</strong></p><pre>void printMessage(String? message) {<br>  print(message ?? &quot;No message&quot;); // Defaults to &quot;No message&quot; if null<br>}</pre><p><strong>Nullable Return Types:</strong></p><pre>String? fetchData() {<br>  return null; // This is allowed<br>}</pre><p><strong>Non-Nullable Return Types:</strong></p><pre>String fetchData() {<br>  return &quot;Hello, World!&quot;; // Must always return a non-null value<br>}</pre><p><strong>7. Null Safety With Collections</strong></p><p>Dart allows you to specify nullability for elements in collections.</p><p><strong>Example:</strong></p><pre>List&lt;int&gt; numbers = [1, 2, 3]; // Non-nullable list<br>List&lt;int?&gt; nullableNumbers = [1, null, 3]; // Nullable elements</pre><p><strong>8. Null Safety and Type Promotion</strong></p><p>Dart can “promote” nullable types to non-nullable if a null check is performed.</p><p><strong>Example:</strong></p><pre>void checkValue(int? value) {<br>  if (value != null) {<br>    print(value + 1); // Type promoted to non-nullable<br>  }<br>}</pre><p><strong>9. Benefits of Null Safety</strong></p><p>• <strong>Compile-time safety:</strong> Many null-related errors are caught during development.</p><p>• <strong>Cleaner code:</strong> Encourages explicit handling of nullable cases.</p><ul><li><strong>Better readability:</strong> Developers can easily understand whether a variable can be null.</li></ul><p>I will soon add a blog on Dart’s <strong>Extensions and Mixins</strong>.</p><p><em>Happy Building, Cheers!</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0666a8a68fcc" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>