Skip to content

Instantly share code, notes, and snippets.

View husen-hn's full-sized avatar
🏠
Working from home

Hossein HassanNejad husen-hn

🏠
Working from home
View GitHub Profile
const cartItem = [
{
title: 'Book1',
exist: false
}, {
title: 'Book2',
exist: true
}, {
title: 'Book3',
exist: true
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
if (message.data['message'] == 'Calling message') {
showCallNotification(
3, message.data['chatroomid'], message.data['userName']);
}
await Firebase.initializeApp();
}
void main() async {
fun main() {
val immutableList = listOf(1,2,3,4,5,6,7) //(1)
println("Immutable List $immutableList")
val mutableList = immutableList.toMutableList() //(2)
println("Mutable List $mutableList")
mutableList.add(8) //(3)
println("Mutable List after add $mutableList")
println("Mutable List after add $immutableList")
}
class MutableObj {
var value = ""
override fun toString(): String {
return "MutableObj(value='$value')"
}
}
fun main() {
val mutableObj:MutableObj = MutableObj()//(1)
val myOtherValue = MyOtherValue("Foo")
myOtherValue.value = "Bar" // This will not compile because value cannot be reassigned
class MyOtherValue(default: String) {
lateinit var value: String
private set
}
val mutable = MyValue.Mutable("Foo")
val immutable = MyValue.Immutable("Foo")
mutable.value = "Bar" // This is fine
immutable.value = "Bar" // This will not compile because value cannot be reassigned
val mutated = immutable.mutate()
mutated.value = "Bar" // This is fine
sealed class MyValue(default: String) {
open val value: String = default
class Immutable(default: String): MyValue(default) {
fun mutate(): Mutable = Mutable(value)
}
class Mutable(default: String) : MyValue(default) {
fun immutate(): Immutable = Immutable(value)
val space = Space()
println(space.all) // result 47
val squares = 30
space.squares = squares * 2
println(space.all) // result 84
class Space{
var squares = 24
var triangles = 23
val all: Int
get() {
return squares + triangles
}
}