Yan
Yan

Reputation: 617

How to define the shape of a map type in Dart

In typescript:

const someMap = {a: "hi", b: 3}; // TS can understand the type of someMap is {a:string, b:number}.

const greet = someMap.a; // TS knows greet is string.

const someMapList: {a:string, b:number}[] = [{a: "hi", b: 3},{a: "bye", b:5}];
someMapList.map((m)=> {
    // TS can understand the shape of m.
})

However in Dart, I only find Map<K,V>. How to achieve the same tasks?

Upvotes: 2

Views: 860

Answers (1)

pedro pimont
pedro pimont

Reputation: 3084

In Typescript/Javascript maps are more like Objects, so it's possible to define their shape like u're trying to. But in Dart that's not the case.

To achieve the same result you must create your on Object

class CustomObject {
  final string a;
  final int b;
  
  CustomObject(this.a, this.b);

}

Upvotes: 2

Related Questions