How to return json data from local storage in flutter app

1,001

You need to convert jsondata[index] that is a map into a Product instance. Normally you would have a fromJson method to create the instance from a json map.

product: Product.fromJson(jsondata[index]),

This is an example of toJson and fromJson implementation

class User {
  final String name;
  final String email;

  User(this.name, this.email);

  User.fromJson(Map<String, dynamic> json)
      : name = json['name'],
        email = json['email'];

  Map<String, dynamic> toJson() =>
    {
      'name': name,
      'email': email,
    };
}
Share:
1,001
aNavi
Author by

aNavi

Updated on December 23, 2022

Comments

  • aNavi
    aNavi over 1 year

    I have json file and i want to display it in Container. I have tried many things but nothing works. For now, I have this error I have tried with jsonDecode(), i know it returns a map like this Map<String, dynamic>, but i don't know how to fix this

    I have Product class and ItemCard class:

    class ItemCard extends StatelessWidget{
      final Product product;
      final Function press;
      const ItemCard({ Key key, this.product, this.press }) : super(key: key);
    
      @override
      Widget build(BuildContext context){
        return GestureDetector(
          onTap: press,
          child: Column(
            children: <Widget>[
              Expanded(
                child: Container(
                  padding: EdgeInsets.all(20),
                  decoration: BoxDecoration(
                    color: Colors.green,
                    borderRadius: BorderRadius.circular(16),
                  ),
                  /*
                  child: Hero(
                    tag: "${product.id}",
                    child: Image.asset(product.image),
                  ),
                  */
                ),
              ),
              Text(product.brand),
            ],
          ),
        );
      }
    }
    

    And in body, my List:

            Container(
              child: Expanded(
                child: FutureBuilder(
                  future: DefaultAssetBundle.of(context)
                    .loadString('assets/watches.json'),
                  builder: (context,snapshot){
    
                    //print(snapshot.data);
                    var jsondata = jsonDecode(snapshot.data.toString());
                    
                      return GridView.builder(
                        itemCount: jsondata.length,
                        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                          crossAxisCount: 2,
                          mainAxisSpacing: 10,
                          crossAxisSpacing: 10,
                          childAspectRatio: 0.75
                        ),
                        itemBuilder: (context, index) => ItemCard(
                          product: jsondata[index],
                          press: (){}
                        ),
                      );
                  },
                ),
              ),
            ),
    

    How can I fix this? any idea would help

  • aNavi
    aNavi almost 4 years
    You mean, I have to write a method separately? with a map argument? thanks for help!!
  • Claudio Redi
    Claudio Redi almost 4 years
    Yes, a method inside the class Product. Pasted an example in my answer