0

From server I am returing json array which contains data of key,value type. I could not find the error. Please help me someone. This is my edited code. I am now getting error: RangeError(index): Index out of range: no Indices are valid: 0

   Future<Products> fetchProducts() async {
     final response =
         await http.get(Uri.parse('url'));

     if (response.statusCode == 200) {
       // If the server did return a 200 OK response,
       // then parse the JSON.
       return Products.fromJson(jsonDecode(response.body));
     } else {
       // If the server did not return a 200 OK response,
       // then throw an exception.
       throw Exception('Failed to load Products');
     }
   }

   class Products {
     final List hotdeals = [];

     Products({
       required List hotdeals,
     });

      factory Products.fromJson(Map json) {
        return Products(hotdeals: json['hotdeals']
        );
      }
   }

And in my MyAppState class (I am showing just necessary portions):

    late Future<Products> futureProducts;

     @override
     void initState() {
       super.initState();
       futureProducts = fetchProducts();
     }

And I am prining like this:

     FutureBuilder<Products>(
       future: futureProducts,
       builder: (context, snapshot) {
          if (snapshot.hasData) {
                              (snapshot.data!.hotdeals[0] as Map)
                                  .forEach((key, value) {
                                print(value[key]['price']);
                              });
                            } else if (snapshot.hasError) {
                      return Text('${snapshot.error}');
                 }

                 // By default, show a loading spinner.
                       return const CircularProgressIndicator();
                    },
                    

My json response is:

   {"hotdeals":[{"id":"10","name":"Platinum 
    Pack","image":"uploads\/1606908813_ppack.jpg","price":"699.00"},{"id":"9","name":"Standard 
    Pack","image":"uploads\/1606908536_spack.jpg","price":"499.00"}]}
Ayan
  • 165
  • 1
  • 1
  • 9
  • If you want data from API so go to the my answer here https://stackoverflow.com/a/68594656/13997210 and here https://stackoverflow.com/a/68533647/13997210 hope it's helpful to you – Ravindra S. Patil Aug 04 '21 at 14:48
  • Try return Products.fromJson(jsonDecode(response.body["hotdeals"])); – Dohan Smit Aug 04 '21 at 14:50
  • Thanks for replying. When I do return Products.fromJson(jsonDecode(response.body['hotdeals']));, fluttter gives error, The argument type 'String' cannnot be assigned to the parameter type 'int'. Why the error coming? – Ayan Aug 05 '21 at 07:49
  • I am the trying the 1st link given you. But an error is giving in this statement: snapshot.data[index]['name']. Flutter is saying that, The method '[]' can't be unconditionally invoked because the receiver can be 'null'. Try making the call conditional (using '?.') or adding a null check to the target ('!'). I do not understand where will I put the checking? – Ayan Aug 05 '21 at 13:19

0 Answers0