Skip to content

Instantly share code, notes, and snippets.

@jasonmcaffee
Created January 25, 2023 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonmcaffee/2ab40e11b5c26f9db51d3bbe9ee5e13c to your computer and use it in GitHub Desktop.
Save jasonmcaffee/2ab40e11b5c26f9db51d3bbe9ee5e13c to your computer and use it in GitHub Desktop.
Dart hashmap memoization
import 'dart:collection';
HashMap memoizedTextThemes = HashMap<String, String>();
abstract class PacificDesignSystem {
String get colorScheme;
String get textTheme {
if(memoizedTextThemes[colorScheme] == null){
memoizedTextThemes[colorScheme] = 'textTheme for $colorScheme';
print('added memoized entry for $colorScheme');
}
return memoizedTextThemes[colorScheme];
}
}
class PacificLight extends PacificDesignSystem {
@override
String get colorScheme => "light";
}
class PacificDark extends PacificDesignSystem {
@override
String get colorScheme => "dark";
}
void main() {
final pLight1 = PacificLight();
final pLight2 = PacificLight();
final pDark1 = PacificDark();
final pDark2 = PacificDark();
print(pLight1.textTheme);
print(pLight1.textTheme);
print(pLight2.textTheme);
print(pLight2.textTheme);
print(pDark1.textTheme);
print(pDark1.textTheme);
print(pDark2.textTheme);
print(pDark2.textTheme);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment