Last active
November 20, 2020 08:41
-
-
Save minikin/a7f52f327eb241a44fc95c4a7b0720e8 to your computer and use it in GitHub Desktop.
Working with optional fields in Swift vs Dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Coffee { | |
String? _temperature; | |
void heat() { | |
_temperature = 'hot'; | |
} | |
void chill() { | |
_temperature = 'iced'; | |
} | |
void checkTemp() { | |
var temperature = _temperature; | |
if (temperature != null) { | |
print('Ready to serve ' + temperature + '!'); | |
} | |
} | |
String serve() => _temperature! + ' coffee'; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Coffee { | |
var temperature: String? | |
mutating func heat() { | |
temperature = "hot" | |
} | |
mutating func chill() { | |
temperature = "iced" | |
} | |
func checkTemp() { | |
guard let unwrappedTemperature = temperature else { | |
print("Temperature is unknown :(") | |
return | |
} | |
print("Ready to serve " + unwrappedTemperature + "!") | |
} | |
func serve() -> String { | |
temperature! + " coffee"; | |
} | |
} | |
let myCoffee = Coffee() | |
myCoffee.checkTemp() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://dart.dev/null-safety/understanding-null-safety#working-with-nullable-fields