Created
July 12, 2018 02:59
-
-
Save jackyq2015/4b0f0c0c35490094736d5910396f956e to your computer and use it in GitHub Desktop.
reproduce the json cast bug. Failed invoking the getRequests function. Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String'
This file contains 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 'dart:convert'; | |
void main() { | |
for (int i = 0; i < 5; i++) { | |
print('hello ${i + 1}'); | |
} | |
String json = | |
"[ { \"customerName\":\"Jack Kal\", \"fromAddress\":\"120 King Street, \", \"fromLocation\":{ \"latitude\":123, \"longitude\":456.88 }, \"item\":\"lipstick\", \"notes\":\"small, pick up by receipeint\", \"price\":5, \"requestTimeStamp\":\"201806301359\", \"requstId\":123456, \"status\":\"pending\", \"toAddress\":\"6666 Harlow Road, L5N4T2, Mississuaga, ON \", \"toLocation\":{ \"latitude\":125, \"longitude\":458.88 } }, { \"customerName\":\"Jet Lee\", \"fromAddress\":\"240 Queen Street, \", \"fromLocation\":{ \"latitude\":1239, \"longitude\":456.88 }, \"item\":\"milk powder\", \"notes\":\"drop off\", \"price\":6, \"requestTimeStamp\":\"201806301959\", \"requstId\":123457, \"status\":\"pending\", \"toAddress\":\"8888 Harlow Road, L5N4T2, Mississuaga, ON \", \"toLocation\":{ \"latitude\":125, \"longitude\":458.88 }, \"weight\":\"8 KG\" } ]"; | |
List<Map<String, dynamic>> data = | |
jsonDecode(json).cast<Map<String, dynamic>>(); | |
print('convert done'); | |
_generateRequests(data); | |
} | |
int _generateRequests(List<Map<String, dynamic>> requestsData) { | |
var listOfRequests = []; | |
for (var postData in requestsData) { | |
listOfRequests.add(new RequestPost.fromJSON(postData)); | |
} | |
return 0; | |
} | |
class RequestPost { | |
final String customerName; | |
final String fromLocation; | |
final String toLocation; | |
final String fromAddress; | |
final String toAddress; | |
final String notes; | |
final String requestId; | |
final String userId; | |
const RequestPost( | |
{this.customerName, | |
this.fromLocation, | |
this.toLocation, | |
this.fromAddress, | |
this.toAddress, | |
this.notes, | |
this.requestId, | |
this.userId}); | |
factory RequestPost.fromJSON(Map<String, dynamic> data) { | |
return new RequestPost( | |
customerName: data['customerName'], | |
fromLocation: data['fromLocation'], | |
toLocation: data['toLocation'], | |
fromAddress: data['fromAddress'], | |
toAddress: data['toAddress'], | |
notes: data['notes'], | |
userId: data['userId'], | |
requestId: data['requestId'], | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment