Flutter: _InternalLinkedHashMap has no instance > method 'cast'
Sthg makes me crazy, I try to show json products in cards and it doesn't work. Here is what I tried so far:
Product class :
class Product {
final String id;
Product({this.id});
factory Product.fromJson(Map<String, dynamic> json) {
return new Product(
id: json['id'] as String
);
}
}
JSON:
Future loadProducts() async {
final response = await http.get('https://api.stripe.com/v1/products');
return response.body;
}
The json has the following structure (data contains a list of products):
Widget:
Widget get _homeView {
return new Column(
children: <Widget>[
new FutureBuilder(
future: loadProducts(),
builder: (context, snapshot) {
List<Product> products = parseJson(snapshot.data.toString());
return !products.isEmpty
? new ProductsList(product: products)
: new CircularProgressIndicator();
}
),
...
]
);
}
List<Product> parseJson(String response) {
final parsed = json.decode(response.toString()).cast<Map<String, dynamic>>();
return parsed.map<Product>((json) => new Product.fromJson(json)).toList();
}
ProductsList class:
class ProductsList extends StatelessWidget {
final List<Product> product;
ProductsList({Key key, this.product}) : super(key: key);
@override
Widget build(BuildContext context) {
return new ListView.builder(
itemCount: product == null ? 0 : product.length,
itemBuilder: (BuildContext context, int index) {
return new Card(
child: new Container(
children: <Widget>[
new Text(product[index].id),
],
)
);
}
);
}
}
Error :
Class '_InternalLinkedHashMap' has no instance
method 'cast' with matching arguments.
Edit 1 :
I tried :
Error :
dart flutter
add a comment |
Sthg makes me crazy, I try to show json products in cards and it doesn't work. Here is what I tried so far:
Product class :
class Product {
final String id;
Product({this.id});
factory Product.fromJson(Map<String, dynamic> json) {
return new Product(
id: json['id'] as String
);
}
}
JSON:
Future loadProducts() async {
final response = await http.get('https://api.stripe.com/v1/products');
return response.body;
}
The json has the following structure (data contains a list of products):
Widget:
Widget get _homeView {
return new Column(
children: <Widget>[
new FutureBuilder(
future: loadProducts(),
builder: (context, snapshot) {
List<Product> products = parseJson(snapshot.data.toString());
return !products.isEmpty
? new ProductsList(product: products)
: new CircularProgressIndicator();
}
),
...
]
);
}
List<Product> parseJson(String response) {
final parsed = json.decode(response.toString()).cast<Map<String, dynamic>>();
return parsed.map<Product>((json) => new Product.fromJson(json)).toList();
}
ProductsList class:
class ProductsList extends StatelessWidget {
final List<Product> product;
ProductsList({Key key, this.product}) : super(key: key);
@override
Widget build(BuildContext context) {
return new ListView.builder(
itemCount: product == null ? 0 : product.length,
itemBuilder: (BuildContext context, int index) {
return new Card(
child: new Container(
children: <Widget>[
new Text(product[index].id),
],
)
);
}
);
}
}
Error :
Class '_InternalLinkedHashMap' has no instance
method 'cast' with matching arguments.
Edit 1 :
I tried :
Error :
dart flutter
The method exists though api.dartlang.org/stable/2.1.0/dart-core/Map/cast.html What Dart version are you using?
– Günter Zöchbauer
Nov 19 '18 at 16:02
I updated my answer.
– Günter Zöchbauer
Nov 19 '18 at 16:04
Just for reference, this article is quite handy for explaining parsing JSON in Flutter and I find myself going back to it every now and then.
– SnakeyHips
Nov 19 '18 at 16:08
I'm using Dart2 @GünterZöchbauer
– Julien
Nov 19 '18 at 16:28
add a comment |
Sthg makes me crazy, I try to show json products in cards and it doesn't work. Here is what I tried so far:
Product class :
class Product {
final String id;
Product({this.id});
factory Product.fromJson(Map<String, dynamic> json) {
return new Product(
id: json['id'] as String
);
}
}
JSON:
Future loadProducts() async {
final response = await http.get('https://api.stripe.com/v1/products');
return response.body;
}
The json has the following structure (data contains a list of products):
Widget:
Widget get _homeView {
return new Column(
children: <Widget>[
new FutureBuilder(
future: loadProducts(),
builder: (context, snapshot) {
List<Product> products = parseJson(snapshot.data.toString());
return !products.isEmpty
? new ProductsList(product: products)
: new CircularProgressIndicator();
}
),
...
]
);
}
List<Product> parseJson(String response) {
final parsed = json.decode(response.toString()).cast<Map<String, dynamic>>();
return parsed.map<Product>((json) => new Product.fromJson(json)).toList();
}
ProductsList class:
class ProductsList extends StatelessWidget {
final List<Product> product;
ProductsList({Key key, this.product}) : super(key: key);
@override
Widget build(BuildContext context) {
return new ListView.builder(
itemCount: product == null ? 0 : product.length,
itemBuilder: (BuildContext context, int index) {
return new Card(
child: new Container(
children: <Widget>[
new Text(product[index].id),
],
)
);
}
);
}
}
Error :
Class '_InternalLinkedHashMap' has no instance
method 'cast' with matching arguments.
Edit 1 :
I tried :
Error :
dart flutter
Sthg makes me crazy, I try to show json products in cards and it doesn't work. Here is what I tried so far:
Product class :
class Product {
final String id;
Product({this.id});
factory Product.fromJson(Map<String, dynamic> json) {
return new Product(
id: json['id'] as String
);
}
}
JSON:
Future loadProducts() async {
final response = await http.get('https://api.stripe.com/v1/products');
return response.body;
}
The json has the following structure (data contains a list of products):
Widget:
Widget get _homeView {
return new Column(
children: <Widget>[
new FutureBuilder(
future: loadProducts(),
builder: (context, snapshot) {
List<Product> products = parseJson(snapshot.data.toString());
return !products.isEmpty
? new ProductsList(product: products)
: new CircularProgressIndicator();
}
),
...
]
);
}
List<Product> parseJson(String response) {
final parsed = json.decode(response.toString()).cast<Map<String, dynamic>>();
return parsed.map<Product>((json) => new Product.fromJson(json)).toList();
}
ProductsList class:
class ProductsList extends StatelessWidget {
final List<Product> product;
ProductsList({Key key, this.product}) : super(key: key);
@override
Widget build(BuildContext context) {
return new ListView.builder(
itemCount: product == null ? 0 : product.length,
itemBuilder: (BuildContext context, int index) {
return new Card(
child: new Container(
children: <Widget>[
new Text(product[index].id),
],
)
);
}
);
}
}
Error :
Class '_InternalLinkedHashMap' has no instance
method 'cast' with matching arguments.
Edit 1 :
I tried :
Error :
dart flutter
dart flutter
edited Nov 19 '18 at 16:14
asked Nov 19 '18 at 15:55
Julien
76221031
76221031
The method exists though api.dartlang.org/stable/2.1.0/dart-core/Map/cast.html What Dart version are you using?
– Günter Zöchbauer
Nov 19 '18 at 16:02
I updated my answer.
– Günter Zöchbauer
Nov 19 '18 at 16:04
Just for reference, this article is quite handy for explaining parsing JSON in Flutter and I find myself going back to it every now and then.
– SnakeyHips
Nov 19 '18 at 16:08
I'm using Dart2 @GünterZöchbauer
– Julien
Nov 19 '18 at 16:28
add a comment |
The method exists though api.dartlang.org/stable/2.1.0/dart-core/Map/cast.html What Dart version are you using?
– Günter Zöchbauer
Nov 19 '18 at 16:02
I updated my answer.
– Günter Zöchbauer
Nov 19 '18 at 16:04
Just for reference, this article is quite handy for explaining parsing JSON in Flutter and I find myself going back to it every now and then.
– SnakeyHips
Nov 19 '18 at 16:08
I'm using Dart2 @GünterZöchbauer
– Julien
Nov 19 '18 at 16:28
The method exists though api.dartlang.org/stable/2.1.0/dart-core/Map/cast.html What Dart version are you using?
– Günter Zöchbauer
Nov 19 '18 at 16:02
The method exists though api.dartlang.org/stable/2.1.0/dart-core/Map/cast.html What Dart version are you using?
– Günter Zöchbauer
Nov 19 '18 at 16:02
I updated my answer.
– Günter Zöchbauer
Nov 19 '18 at 16:04
I updated my answer.
– Günter Zöchbauer
Nov 19 '18 at 16:04
Just for reference, this article is quite handy for explaining parsing JSON in Flutter and I find myself going back to it every now and then.
– SnakeyHips
Nov 19 '18 at 16:08
Just for reference, this article is quite handy for explaining parsing JSON in Flutter and I find myself going back to it every now and then.
– SnakeyHips
Nov 19 '18 at 16:08
I'm using Dart2 @GünterZöchbauer
– Julien
Nov 19 '18 at 16:28
I'm using Dart2 @GünterZöchbauer
– Julien
Nov 19 '18 at 16:28
add a comment |
1 Answer
1
active
oldest
votes
This is my usual method for parsing a json list of objects (bit simpler but it works):
List<Product> parseJson(String response) {
List<Product> products = new List<Product>();
List jsonParsed = json.decode(response.toString());
for (int i = 0; i < jsonParsed.length; i++) {
products.add(new Product.fromJson(jsonParsed[i]));
}
return products;
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378341%2fflutter-internallinkedhashmap-has-no-instance-method-cast%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is my usual method for parsing a json list of objects (bit simpler but it works):
List<Product> parseJson(String response) {
List<Product> products = new List<Product>();
List jsonParsed = json.decode(response.toString());
for (int i = 0; i < jsonParsed.length; i++) {
products.add(new Product.fromJson(jsonParsed[i]));
}
return products;
}
add a comment |
This is my usual method for parsing a json list of objects (bit simpler but it works):
List<Product> parseJson(String response) {
List<Product> products = new List<Product>();
List jsonParsed = json.decode(response.toString());
for (int i = 0; i < jsonParsed.length; i++) {
products.add(new Product.fromJson(jsonParsed[i]));
}
return products;
}
add a comment |
This is my usual method for parsing a json list of objects (bit simpler but it works):
List<Product> parseJson(String response) {
List<Product> products = new List<Product>();
List jsonParsed = json.decode(response.toString());
for (int i = 0; i < jsonParsed.length; i++) {
products.add(new Product.fromJson(jsonParsed[i]));
}
return products;
}
This is my usual method for parsing a json list of objects (bit simpler but it works):
List<Product> parseJson(String response) {
List<Product> products = new List<Product>();
List jsonParsed = json.decode(response.toString());
for (int i = 0; i < jsonParsed.length; i++) {
products.add(new Product.fromJson(jsonParsed[i]));
}
return products;
}
answered Nov 19 '18 at 16:29
SnakeyHips
550111
550111
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378341%2fflutter-internallinkedhashmap-has-no-instance-method-cast%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
The method exists though api.dartlang.org/stable/2.1.0/dart-core/Map/cast.html What Dart version are you using?
– Günter Zöchbauer
Nov 19 '18 at 16:02
I updated my answer.
– Günter Zöchbauer
Nov 19 '18 at 16:04
Just for reference, this article is quite handy for explaining parsing JSON in Flutter and I find myself going back to it every now and then.
– SnakeyHips
Nov 19 '18 at 16:08
I'm using Dart2 @GünterZöchbauer
– Julien
Nov 19 '18 at 16:28