Created
January 26, 2024 05:05
-
-
Save vi-k/2470abdbbd93b3d31e72acaae4c03be4 to your computer and use it in GitHub Desktop.
UniqueKey test
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
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(const MyApp()); | |
} | |
class SomeObject { | |
final String title; | |
const SomeObject(this.title); | |
} | |
class MyApp extends StatefulWidget { | |
const MyApp({super.key}); | |
@override | |
State<MyApp> createState() => _MyAppState(); | |
} | |
class _MyAppState extends State<MyApp> { | |
static const uniqueObjects = [ | |
SomeObject('1'), | |
SomeObject('2'), | |
SomeObject('3'), | |
SomeObject('4'), | |
]; | |
static final initialObjectsForView = [ | |
uniqueObjects[0], | |
uniqueObjects[1], | |
uniqueObjects[1], | |
uniqueObjects[2], | |
uniqueObjects[2], | |
uniqueObjects[2], | |
uniqueObjects[3], | |
uniqueObjects[3], | |
uniqueObjects[3], | |
uniqueObjects[3], | |
]; | |
late final Map<Key, SomeObject> mapObjectsForView = { | |
for (final obj in initialObjectsForView) UniqueKey(): obj, | |
}; | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
debugShowCheckedModeBanner: false, | |
home: Scaffold( | |
appBar: AppBar(title: const Text('Some objects')), | |
body: Center( | |
child: Column( | |
children: [ | |
for (final MapEntry(:key, :value) in mapObjectsForView.entries) | |
ElevatedButton( | |
key: key, | |
onPressed: () { | |
setState(() { | |
mapObjectsForView.remove(key); | |
}); | |
}, | |
child: Text('${value.title} @ ${key.hashCode}'), | |
) | |
], | |
), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment