I would like to use the Spread operator (...) on key-value-data with type safety. In TypeScript I would achieve this with Interfaces, but I could not figure out a way of doing this in dart.
Failed approach 1: Use Classes
class Foo {
int aNumber;
String aString;
}
Foo a = Foo();
Foo b = { ...a, 'aString': 'ipsum' }; // This literal must be either a map or a set.
Failed approach 2: Use Maps
Map<String, dynamic> a = { 'aNumber': 2, 'aString': 'lorem' };
Map<String, dynamic> b = { ...a, 'aString': 'ipsum' }; // No error, but also no type safety.
Note: In these approaches, b should not be a list.
TypeScript Example of what I need
interface Foo {
aNumber: number;
aString: string;
}
const a: Foo = { aNumber: 2, aString: 'lorem' };
const b: Foo = { ...a, aString: 'ipsum' }; // Has all the props of 'a' and overrides 'aString' to 'ipsum'.