{"id":1775,"date":"2024-02-29T12:29:02","date_gmt":"2024-02-29T12:29:02","guid":{"rendered":"https:\/\/www.devopssupport.in\/blog\/?p=1775"},"modified":"2024-03-02T12:31:32","modified_gmt":"2024-03-02T12:31:32","slug":"flutter-conditional-statements-if-else","status":"publish","type":"post","link":"https:\/\/www.devopssupport.in\/blog\/flutter-conditional-statements-if-else\/","title":{"rendered":"Flutter: Conditional Statements If-Else"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2024\/03\/image-23.png\" alt=\"\" class=\"wp-image-1776\" width=\"415\" height=\"396\" srcset=\"https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2024\/03\/image-23.png 356w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2024\/03\/image-23-300x287.png 300w\" sizes=\"auto, (max-width: 415px) 100vw, 415px\" \/><\/figure>\n\n\n\n<p>The if-else statement is a fundamental construct in programming that allows you to execute different blocks of code based on specific conditions. In Flutter, if-else statements are used extensively to control the flow of your application&#8217;s logic and user interface.<\/p>\n\n\n\n<p><strong>Syntax of If-Else Statements in Dart (Flutter&#8217;s Programming Language):<\/strong><\/p>\n\n\n\n<p>The syntax of an if-else statement in Dart is as follows.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (condition) {\r\n  \/\/ Code to execute if the condition is true\r\n} else {\r\n  \/\/ Code to execute if the condition is false\r\n}\r\n<\/code><\/pre>\n\n\n\n<p>In this syntax:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>condition<\/code> is a boolean expression that evaluates to either true or false.<\/li>\n\n\n\n<li>The code block inside the <code>if<\/code> statement is executed if the condition is true.<\/li>\n\n\n\n<li>The code block inside the <code>else<\/code> statement is executed if the condition is false.<\/li>\n<\/ul>\n\n\n\n<p><strong>Example: Using If-Else Statements in Flutter Widgets:<\/strong><\/p>\n\n\n\n<p>Let&#8217;s consider a simple example where we want to display a message based on whether a user is logged in or not. We can achieve this using if-else statements in Flutter widgets.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import 'package:flutter\/material.dart';\r\n\r\nclass HomePage extends StatelessWidget {\r\n  final bool isLoggedIn = true;\r\n\r\n  @override\r\n  Widget build(BuildContext context) {\r\n    return Scaffold(\r\n      appBar: AppBar(\r\n        title: Text('Conditional Statements in Flutter'),\r\n      ),\r\n      body: Center(\r\n        child: isLoggedIn\r\n            ? Text('Welcome, User!')\r\n            : Text('Please log in to continue.'),\r\n      ),\r\n    );\r\n  }\r\n}\r\n\r\nvoid main() {\r\n  runApp(MaterialApp(\r\n    home: HomePage(),\r\n  ));\r\n}\r<\/code><\/pre>\n\n\n\n<p>We define a boolean variable <code>isLoggedIn<\/code> that determines whether the user is logged in.<\/p>\n\n\n\n<p>Inside the <code>build<\/code> method of the <code>HomePage<\/code> widget, we use an if-else statement to conditionally display different messages based on the value of <code>isLoggedIn<\/code>.<\/p>\n\n\n\n<p>If <code>isLoggedIn<\/code> is true, we display a welcome message. Otherwise, we prompt the user to log in.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The if-else statement is a fundamental construct in programming that allows you to execute different blocks of code based on specific conditions. In Flutter, if-else statements are&#8230; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1775","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/1775","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/comments?post=1775"}],"version-history":[{"count":1,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/1775\/revisions"}],"predecessor-version":[{"id":1777,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/1775\/revisions\/1777"}],"wp:attachment":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/media?parent=1775"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/categories?post=1775"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/tags?post=1775"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}