Flutter: _InternalLinkedHashMap has no instance > method 'cast'












0














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):



enter image description here



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 :
enter image description here
Error :
enter image description here










share|improve this question
























  • 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
















0














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):



enter image description here



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 :
enter image description here
Error :
enter image description here










share|improve this question
























  • 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














0












0








0







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):



enter image description here



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 :
enter image description here
Error :
enter image description here










share|improve this question















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):



enter image description here



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 :
enter image description here
Error :
enter image description here







dart flutter






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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












1 Answer
1






active

oldest

votes


















0














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;
}





share|improve this answer





















    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    0














    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;
    }





    share|improve this answer


























      0














      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;
      }





      share|improve this answer
























        0












        0








        0






        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;
        }





        share|improve this answer












        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;
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 '18 at 16:29









        SnakeyHips

        550111




        550111






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            SQL update select statement

            'app-layout' is not a known element: how to share Component with different Modules