This problem bothered me until I thought that I could implement a crude from method to handle the conversion of encoded Json or Dart Maps objects / strings to the desired class.
Below is a simple example that also processes null values ββand accepts JSON (as a string parameter).
import 'dart:convert'; class PaymentDetail { String AccountNumber; double Amount; int ChargeTypeID; String CustomerNames; PaymentDetail({ this.AccountNumber, this.Amount, this.ChargeTypeID, this.CustomerNames }); PaymentDetail from ({ string : String, object : Map }) { var map = (object==null) ? (string==null) ? Map() : json.decode(string) : (object==null) ? Map() : object; return new PaymentDetail( AccountNumber : map["AccountNumber"] as String, Amount : map["Amount"] as double, ChargeTypeID : map["ChargeTypeID"] as int, CustomerNames : map["CustomerNames"] as String ); } }
Below is the implementation
PaymentDetail payDetail = new PaymentDetail().from(object: new Map()); PaymentDetail otherPayDetail = new PaymentDetail().from(object: {"AccountNumber": "1234", "Amount": 567.2980908});
Once again, it is easy and tedious to clone an entire project, but it works for simple cases.
source share