├── .github ├── FUNDING.yml ├── ISSUE_TEMPLATE │ ├── bug_report.md │ ├── build.md │ ├── chore.md │ ├── ci.md │ ├── config.yml │ ├── documentation.md │ ├── feature_request.md │ ├── performance.md │ ├── refactor.md │ ├── revert.md │ ├── style.md │ └── test.md ├── PULL_REQUEST_TEMPLATE.md ├── dependabot.yaml └── workflows │ ├── master.yml │ └── release.yml ├── LICENSE ├── README.md └── code-samples.dart /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | patreon: user?u=84015491 # Replace with a single Patreon username 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry 13 | custom: 14 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug Report 3 | about: Create a report to help us improve 4 | title: "fix: " 5 | labels: bug 6 | --- 7 | 8 | **Description** 9 | 10 | A clear and concise description of what the bug is. 11 | 12 | **Steps To Reproduce** 13 | 14 | 1. Go to '...' 15 | 2. Click on '....' 16 | 3. Scroll down to '....' 17 | 4. See error 18 | 19 | **Expected Behavior** 20 | 21 | A clear and concise description of what you expected to happen. 22 | 23 | **Screenshots** 24 | 25 | If applicable, add screenshots to help explain your problem. 26 | 27 | **Additional Context** 28 | 29 | Add any other context about the problem here. 30 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/build.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Build System 3 | about: Changes that affect the build system or external dependencies 4 | title: "build: " 5 | labels: build 6 | --- 7 | 8 | **Description** 9 | 10 | Describe what changes need to be done to the build system and why. 11 | 12 | **Requirements** 13 | 14 | - [ ] The build system is passing 15 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/chore.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Chore 3 | about: Other changes that don't modify src or test files 4 | title: "chore: " 5 | labels: chore 6 | --- 7 | 8 | **Description** 9 | 10 | Clearly describe what change is needed and why. If this changes code then please use another issue type. 11 | 12 | **Requirements** 13 | 14 | - [ ] No functional changes to the code 15 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/ci.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Continuous Integration 3 | about: Changes to the CI configuration files and scripts 4 | title: "ci: " 5 | labels: ci 6 | --- 7 | 8 | **Description** 9 | 10 | Describe what changes need to be done to the ci/cd system and why. 11 | 12 | **Requirements** 13 | 14 | - [ ] The ci system is passing 15 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/config.yml: -------------------------------------------------------------------------------- 1 | blank_issues_enabled: false -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/documentation.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Documentation 3 | about: Improve the documentation so all collaborators have a common understanding 4 | title: "docs: " 5 | labels: documentation 6 | --- 7 | 8 | **Description** 9 | 10 | Clearly describe what documentation you are looking to add or improve. 11 | 12 | **Requirements** 13 | 14 | - [ ] Requirements go here 15 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature Request 3 | about: A new feature to be added to the project 4 | title: "feat: " 5 | labels: feature 6 | --- 7 | 8 | **Description** 9 | 10 | Clearly describe what you are looking to add. The more context the better. 11 | 12 | **Requirements** 13 | 14 | - [ ] Checklist of requirements to be fulfilled 15 | 16 | **Additional Context** 17 | 18 | Add any other context or screenshots about the feature request go here. 19 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/performance.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Performance Update 3 | about: A code change that improves performance 4 | title: "perf: " 5 | labels: performance 6 | --- 7 | 8 | **Description** 9 | 10 | Clearly describe what code needs to be changed and what the performance impact is going to be. Bonus point's if you can tie this directly to user experience. 11 | 12 | **Requirements** 13 | 14 | - [ ] There is no drop in test coverage. 15 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/refactor.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Refactor 3 | about: A code change that neither fixes a bug nor adds a feature 4 | title: "refactor: " 5 | labels: refactor 6 | --- 7 | 8 | **Description** 9 | 10 | Clearly describe what needs to be refactored and why. Please provide links to related issues (bugs or upcoming features) in order to help prioritize. 11 | 12 | **Requirements** 13 | 14 | - [ ] There is no drop in test coverage. 15 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/revert.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Revert Commit 3 | about: Reverts a previous commit 4 | title: "revert: " 5 | labels: revert 6 | --- 7 | 8 | **Description** 9 | 10 | Provide a link to a PR/Commit that you are looking to revert and why. 11 | 12 | **Requirements** 13 | 14 | - [ ] Change has been reverted 15 | - [ ] No change in test coverage has happened 16 | - [ ] A new ticket is created for any follow on work that needs to happen 17 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/style.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Style Changes 3 | about: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc) 4 | title: "style: " 5 | labels: style 6 | --- 7 | 8 | **Description** 9 | 10 | Clearly describe what you are looking to change and why. 11 | 12 | **Requirements** 13 | 14 | - [ ] There is no drop in test coverage. 15 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/test.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Test 3 | about: Adding missing tests or correcting existing tests 4 | title: "test: " 5 | labels: test 6 | --- 7 | 8 | **Description** 9 | 10 | List out the tests that need to be added or changed. Please also include any information as to why this was not covered in the past. 11 | 12 | **Requirements** 13 | 14 | - [ ] There is no drop in test coverage. 15 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 8 | 9 | ## Status 10 | 11 | **READY/IN DEVELOPMENT/HOLD** 12 | 13 | ## Description 14 | 15 | 16 | 17 | ## Type of Change 18 | 19 | 20 | 21 | - [ ] ✨ New feature (non-breaking change which adds functionality) 22 | - [ ] 🛠️ Bug fix (non-breaking change which fixes an issue) 23 | - [ ] ❌ Breaking change (fix or feature that would cause existing functionality to change) 24 | - [ ] 🧹 Code refactor 25 | - [ ] ✅ Build configuration change 26 | - [ ] 📝 Documentation 27 | - [ ] 🗑️ Chore 28 | -------------------------------------------------------------------------------- /.github/dependabot.yaml: -------------------------------------------------------------------------------- 1 | version: 2 2 | enable-beta-ecosystems: true 3 | updates: 4 | - package-ecosystem: "github-actions" 5 | directory: "/" 6 | schedule: 7 | interval: "daily" 8 | - package-ecosystem: "pub" 9 | directory: "/" 10 | schedule: 11 | interval: "daily" 12 | -------------------------------------------------------------------------------- /.github/workflows/master.yml: -------------------------------------------------------------------------------- 1 | name: ci 2 | 3 | concurrency: 4 | group: $-$ 5 | cancel-in-progress: true 6 | 7 | on: 8 | pull_request: 9 | branches: 10 | - master 11 | 12 | jobs: 13 | semantic_pull_request: 14 | uses: VeryGoodOpenSource/very_good_workflows/.github/workflows/semantic_pull_request.yml@v1 15 | 16 | pana: 17 | runs-on: ubuntu-latest 18 | steps: 19 | - name: noop 20 | run: echo 'noop' -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | # https://docs.github.com/en/actions 2 | 3 | name: "Release" 4 | 5 | on: # yamllint disable-line rule:truthy 6 | push: 7 | tags: 8 | - "**" 9 | 10 | jobs: 11 | release: 12 | name: "Release" 13 | 14 | runs-on: "ubuntu-latest" 15 | 16 | steps: 17 | - name: "Determine tag" 18 | run: "echo \"RELEASE_TAG=${GITHUB_REF#refs/tags/}\" >> $GITHUB_ENV" 19 | 20 | - name: "Create release" 21 | uses: "actions/github-script@v7" 22 | with: 23 | github-token: "${{ secrets.ERGEBNIS_BOT_TOKEN }}" 24 | script: | 25 | try { 26 | await github.rest.repos.createRelease({ 27 | draft: false, 28 | generate_release_notes: true, 29 | name: process.env.RELEASE_TAG, 30 | owner: context.repo.owner, 31 | prerelease: false, 32 | repo: context.repo.repo, 33 | tag_name: process.env.RELEASE_TAG, 34 | }); 35 | } catch (error) { 36 | core.setFailed(error.message); 37 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Babalola Ayotomide(czar) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Welcome to the "WTF Dart" repository! 2 | 3 | inspired by the [wtfjs](https://github.com/denysdovhan/wtfjs) repository 4 | 5 | This repository is a collection of Dart code snippets and examples that may make you say "WTF" (What the F\*ck) at first glance. However, these snippets are meant to be educational and demonstrate some of the more unusual or unexpected behaviors of the Dart programming language. 6 | 7 | ## How to Use 8 | 9 | To use this repository, simply clone or download the code and explore the examples. Every code is labeled with a short description of what it demonstrates. You can run the code using the Dart SDK, which can be downloaded from the [official Dart website](https://dart.dev/get-dart). 10 | 11 | It's important to note that some of these examples may not be suitable for production use, and should be used with caution. Additionally, some of the examples may have unintended consequences or may not behave as you expect, so be sure to read through the code and understand what it's doing before using it in your own projects. 12 | 13 | ## Contributing 14 | 15 | If you have your own "WTF" Dart examples that you'd like to share, feel free to submit a pull request! We welcome contributions from the community, and we're always looking for new and interesting examples to add to the repository. 16 | 17 | Please be sure to include a brief description of what your example demonstrates, as well as any notes or warnings about potential pitfalls or unexpected behavior. 18 | 19 | # Table of Contents 20 | 21 | - [💪🏻 Motivation](#-motivation) 22 | - [👀 Examples](#-examples) 23 | - [null is not null](#null-is-not-null) 24 | - [This code does nothing](#this-code-does-nothing) 25 | - [Switch case fall-through](#switch-case-fall-through) 26 | - [Recursive function with no base case](#recursive-function-with-no-base-case) 27 | - [Type Mismatch and Comparison Error](#type-mismatch-and-comparison-error) 28 | - [String indexing](#string-indexing) 29 | - [Using `is` with `!`](#using-is-with--) 30 | - [Function with optional positional arguments](#function-with-optional-positional-arguments) 31 | - [Using `await` without `async`](#using--without--) 32 | - [Mixing `var` and `final`](#mixing-var-and-final) 33 | - [Comparing doubles](#comparing-doubles) 34 | - [Using `as` with `null`](#using-as-with-null) 35 | - [Accessing private members](#accessing-private-members) 36 | - [Using `assert` with side effects](#using-assert-with-side-effects) 37 | - [Using `try` without `catch` or `finally`](#using-try-without-catch-or-finally) 38 | - [Method Chaining with the Cascade Operator](#method-chaining-with-the-cascade-operator) 39 | - [Operator Precedence and Grouping](#operator-precedence-and-grouping) 40 | - [Copying Maps with `Map.from()`](copying-maps-with--) 41 | - [String Concatenation without the + Operator](#string-concatenation-without-the-+-Operator) 42 | - [Surprising Results with `num.parse()`](<#surprising-results-with-num.parse()>) 43 | - [String Interpolation with Conditional Expressions](#string-interpolation-with-conditional-expressions) 44 | - [Behavior with Optional Variables](#behavior-with-optional-variables) 45 | 46 | # 💪🏻 Motivation 47 | 48 | > "Programming is not about what you know, it's about what you can figure out." 49 | > 50 | > — _Chris Pine_ 51 | 52 | WTF-Dart is a repository created with the goal of showcasing surprising or unconventional behavior in Dart programming language. The repository contains a collection of Dart code snippets that demonstrate unexpected results or non-intuitive behavior in the language, which can be useful for developers who are learning Dart or working with it on a regular basis. 53 | 54 | By exploring these code snippets and learning about the quirks and surprises of the Dart language, developers can gain a deeper understanding of how the language works and how to avoid potential pitfalls. This repository is also a valuable resource for educators who teach Dart programming, as it provides real-world examples of how the language can behave in unexpected ways. 55 | 56 | Ultimately, WTF-Dart aims to help developers write more reliable and efficient code in Dart by providing them with a deeper understanding of the language's behavior. Whether you're a beginner or an experienced developer, this repository is a great resource for learning about the nuances of the Dart language and improving your coding skills. 57 | 58 | > **⚠️ Note:** If you enjoy reading this document, please, [consider supporting the author of this collection](#-supporting). 59 | 60 | ## Null is not null 61 | 62 | ```dart 63 | var a = Null; 64 | if (a != null) { 65 | print("null is not null"); 66 | } 67 | ``` 68 | 69 | ### 💡 Explanation: 70 | 71 | "Wait, so even though a is assigned the value Null, the condition a != null still evaluates to true? That doesn't make any sense! How can Null not be equal to null?" 72 | 73 | The reason for this behavior is that Null is actually a type in Dart, not a value. When you assign a variable the value Null, you're actually assigning it the null value of the variable's declared type (which is usually dynamic if you don't specify a type). So even though a is assigned the value Null, its type is still dynamic, and the condition a != null evaluates to true because a has a non-null value (even though that value happens to be null). This can be confusing if you're not familiar with Dart's type system and null safety. 74 | 75 | ## This code does nothing 76 | 77 | ```dart 78 | void main() { 79 | while(true); 80 | } 81 | ``` 82 | 83 | ### 💡 Explanation: 84 | 85 | This code may seem like an infinite loop, but it actually does nothing because the loop condition is always true and there is no code inside the loop. This can be a WTF moment if you're not expecting it. 86 | 87 | `Disclaimer:` The above code crashed my Dart Pad. 88 | 89 | ## Switch case fall-through 90 | 91 | ```dart 92 | void main() { 93 | var i = 2; 94 | switch (i) { 95 | case 1: 96 | print("One"); 97 | break; 98 | case 2: 99 | case 3: 100 | print("Two or Three"); 101 | break; 102 | default: 103 | print("Something else"); 104 | } 105 | } 106 | ``` 107 | 108 | ### 💡 Explanation: 109 | 110 | In Dart, switch cases can fall through to the next case unless a break statement is used. This code will print "Two or Three" because the case 2: statement falls through to case 3: 111 | 112 | ## Recursive function with no base case 113 | 114 | ```dart 115 | void main() { 116 | int factorial(int n) { 117 | return n * factorial(n - 1); 118 | } 119 | 120 | print(factorial(5)); 121 | } 122 | ``` 123 | 124 | ### 💡 Explanation: 125 | 126 | This code defines a recursive function to calculate the factorial of a number, but there is no base case to stop the recursion. When factorial(0) is called, the function will continue to call itself with negative numbers until a stack overflow error occurs. 127 | 128 | ## Type Mismatch and Comparison Error 129 | 130 | ```dart 131 | void main() { 132 | var x = 10; 133 | var y = "20"; 134 | 135 | if (x < y) { 136 | print("x is less than y"); 137 | } 138 | } 139 | ``` 140 | 141 | ### 💡 Explanation: 142 | 143 | At first glance, this code might seem reasonable: you're comparing two values to see which one is less. However, it will actually throw a runtime error because x is an integer and y is a string. In Dart, you cannot directly compare values of different types, so attempting to do so will result in a type error. This can be a WTF moment if you're not expecting it. 144 | 145 | ## String indexing 146 | 147 | ```dart 148 | void main() { 149 | var str = "Hello"; 150 | print(str[0]); // Prints "H" 151 | } 152 | ``` 153 | 154 | ### 💡 Explanation: 155 | 156 | In Dart, you can access individual characters of a string using square brackets, as if the string were an array. This can be a WTF moment if you're coming from a language that doesn't allow string indexing. 157 | 158 | ## Using `is` with `!` 159 | 160 | ```dart 161 | void main() { 162 | var a = "Hello"; 163 | if (a is! int) { 164 | print("a is not an int"); 165 | } 166 | } 167 | ``` 168 | 169 | ### 💡 Explanation: 170 | 171 | In Dart, you can use the `is` operator to check the type of a variable, and you can negate the result using `!`. This code will print "a is not an int" because `a` is a string. 172 | 173 | ## Function with optional positional arguments 174 | 175 | ```dart 176 | void main() { 177 | void greet(String name, [String greeting = "Hello"]) { 178 | print("$greeting, $name!"); 179 | } 180 | 181 | greet("Czar"); // Prints "Hello, Czar!" 182 | greet("Czar", "Hi"); // Prints "Hi, Czar!" 183 | } 184 | ``` 185 | 186 | ### 💡 Explanation: 187 | 188 | In Dart, you can define optional positional arguments by enclosing them in square brackets. This can be a WTF moment if you're not expecting optional arguments. 189 | 190 | ## Using `await` without `async` 191 | 192 | ```dart 193 | Future getData() async { 194 | return "Data"; 195 | } 196 | 197 | void main() { 198 | var data = await getData(); 199 | print(data); 200 | } 201 | ``` 202 | 203 | ### 💡 Explanation: 204 | 205 | In Dart, the await keyword is used to wait for a Future to complete, but it can only be used inside an async function. This code will produce a compiler error because main() is not declared as async. 206 | 207 | ## Mixing `var` and `final` 208 | 209 | ```dart 210 | void main() { 211 | final var a = "Hello"; 212 | print(a); 213 | } 214 | ``` 215 | 216 | ### 💡 Explanation: 217 | 218 | In Dart, `var` is used to declare a variable with inferred type, and final is used to declare a variable that can only be assigned once. This code will produce a compiler error because `var` and `final` cannot be used together. 219 | 220 | ## Comparing doubles 221 | 222 | ```dart 223 | void main() { 224 | var a = 0.1 + 0.2; 225 | var b = 0.3; 226 | if (a == b) { 227 | print("Equal!"); 228 | } else { 229 | print("Not equal!"); 230 | } 231 | } 232 | ``` 233 | 234 | ### 💡 Explanation: 235 | 236 | In Dart, comparing floating-point numbers can produce unexpected results due to the way they are represented in memory. This code will print "Not equal!" even though mathematically `0.1 + 0.2` is equal to `0.3`. 237 | 238 | ## Using `as` with `null` 239 | 240 | ```dart 241 | void main() { 242 | var a = null; 243 | print(a as String); 244 | } 245 | ``` 246 | 247 | ### 💡 Explanation: 248 | 249 | In Dart, you can use the `as` operator to cast a variable to a certain type, but it will throw a runtime error if the cast fails. This code will throw a `TypeError` because `a` is `null` and cannot be cast to `String`. 250 | 251 | ## Accessing private members 252 | 253 | ```dart 254 | class Person { 255 | String _name = "czar"; 256 | } 257 | 258 | void main() { 259 | var p = Person(); 260 | print(p._name); 261 | } 262 | ``` 263 | 264 | ### 💡 Explanation: 265 | 266 | In Dart, you can use an underscore prefix to mark a member as private, but it is still accessible from outside the class. This code will print "czar" even though `_name` is marked as private. 267 | 268 | ## Using `assert` with side effects 269 | 270 | ```dart 271 | void main() { 272 | var a = 0; 273 | assert((a += 1) == 1); 274 | print(a); 275 | } 276 | ``` 277 | 278 | ### 💡 Explanation: 279 | 280 | In Dart, you can use the `assert` keyword to check a condition during development, but it should not have any side effects. This code will print "1" even though `a` was modified by the` assert` statement. 281 | 282 | ## Using `try` without `catch` or `finally` 283 | 284 | ```dart 285 | void main() { 286 | try { 287 | print("Hello"); 288 | } 289 | } 290 | ``` 291 | 292 | ### 💡 Explanation: 293 | 294 | In Dart, you can use the `try` keyword to enclose code that may throw an exception, but you must also use either catch or finally to handle the exception. This code will produce a compiler error because try is not followed by catch or finally 295 | 296 | ## Method Chaining with the Cascade Operator 297 | 298 | ```dart 299 | class Person { 300 | String? name; 301 | int? age; 302 | void greet() => print("Hello, my name is $name and I'm $age years old."); 303 | } 304 | 305 | void main() { 306 | var person = Person() 307 | ..name = "Czar" 308 | ..age = 17 309 | ..greet(); 310 | } 311 | ``` 312 | 313 | ### 💡 Explanation: 314 | 315 | This will actually print "Hello, my name is Czar and I'm 17 years old.", because the cascade notation allows you to call the greet() method on the same object that you're setting the name and age properties on. This can be a bit confusing if you're not familiar with the cascade notation syntax. 316 | 317 | ## Operator Precedence and Grouping 318 | 319 | ```dart 320 | void main() { 321 | int x = 1; 322 | int y = 2; 323 | print(x > 0 || y > 0 && x < y); // Output: true 324 | } 325 | ``` 326 | 327 | ### 💡 Explanation: 328 | 329 | In this code, the `||` and `&&` operators are used to combine multiple boolean expressions. However, the order of evaluation can be unexpected. In this case, the `&&` operator has higher precedence than the `||` operator, so `y > 0 && x < y` is evaluated first, resulting in `true`. Then, `true || true` is evaluated, resulting in `true`. This can be a WTF moment if you're not familiar with operator precedence in Dart. 330 | 331 | ## Copying Maps with `Map.from()` 332 | 333 | ```dart 334 | void main() { 335 | var source = {"a": 1, "b": 2}; 336 | var copy = Map.from(source); 337 | copy["a"] = 3; 338 | print(source); // Output: {a: 1, b: 2} 339 | print(copy); // Output: {a: 3, b: 2} 340 | } 341 | ``` 342 | 343 | ### 💡 Explanation: 344 | 345 | In this code, `Map.from()` is used to create a copy of a map `source`. However, when a value is updated in the copy, the original map is not affected. This can be a WTF moment if you're expecting the two maps to be linked together. 346 | 347 | ## String Concatenation without the + Operator 348 | 349 | ```dart 350 | void main() { 351 | String s = "hello" " world";// Output: hello world 352 | print(s); 353 | } 354 | ``` 355 | 356 | ### 💡 Explanation: 357 | 358 | In Dart, you can concatenate strings using the `+` operator, just like in many other programming languages. However, you can also concatenate strings by simply placing them next to each other, without any operator between them. 359 | 360 | In the above code, the string "hello" and "world" are placed next to each other without any operator between them. The resulting string is "hello world", which is printed to the console. This can be a WTF moment if you're not familiar with this syntax in Dart. 361 | 362 | ## Surprising Results with `num.parse()` 363 | 364 | ```dart 365 | void main() { 366 | String str1 = '42'; 367 | String str2 = '3.14159'; 368 | String str3 = '0x42'; 369 | int intVal = num.parse(str1).toInt(); 370 | double doubleVal = num.parse(str2).toDouble(); 371 | int hexVal = num.parse(str3).toInt(); 372 | print(intVal); // Output: 42 373 | print(doubleVal); // Output: 3.14159 374 | print(hexVal); // Output: 66 375 | } 376 | ``` 377 | 378 | ### 💡 Explanation: 379 | 380 | This Dart code snippet demonstrates how to use the `num.parse()` method to convert strings to numbers. However, the results can be surprising due to the way Dart handles different types of numbers. In this case, the code parses three different strings as integers, doubles, and hexadecimal integers, respectively, producing unexpected output values for each. 381 | 382 | ## String Interpolation with Conditional Expressions 383 | 384 | ```dart 385 | void main() { 386 | bool isSunny = true; 387 | String message = 'It is ${isSunny ? 'sunny' : 'cloudy'} today.'; 388 | print(message); // Output: It is sunny today. 389 | } 390 | ``` 391 | 392 | ### 💡 Explanation: 393 | 394 | This Dart code snippet showcases the use of conditional expressions in string interpolation. It demonstrates how the result of a conditional expression can be directly included in a string using the "${expression ? trueCase : falseCase}" syntax. In this example, the value of the boolean variable `isSunny` determines whether the string "sunny" or "cloudy" is included in the resulting message. 395 | 396 | ## Behavior with Optional Variables 397 | 398 | ```dart 399 | void main() { 400 | String? message; 401 | print(message?.toUpperCase()); // Output: null 402 | } 403 | ``` 404 | 405 | ### 💡 Explanation: 406 | 407 | This Dart code snippet demonstrates the use of the null-aware operator with an optional variable. The null-aware operator "?." is used to call the `toUpperCase()` method on the `message` string variable, which is declared as nullable using the `String?` syntax. However, since the message variable is null, the output of the expression is also null, which may be unexpected to developers who are not familiar with the null-aware operator. 408 | 409 | # 🤝 Supporting 410 | 411 | | Service | Link | Action | 412 | | ----------- | :-------------: | :------------------------------------------------------------------------------------------------------------------------------------------------: | 413 | | **Patreon** | Become a patron | | 414 | 415 | ## License 416 | 417 | This repository is licensed under the MIT License. See the [LICENSE](LICENSE) file for more information. 418 | -------------------------------------------------------------------------------- /code-samples.dart: -------------------------------------------------------------------------------- 1 | // add code samples here 2 | --------------------------------------------------------------------------------