0
votes

I'm new in Flutter , I'm trying to fetch api data, got the following error type 'String' is not a subtype of type 'int' of 'index' , any help, please?

type 'String' is not a subtype of type 'int' of 'index' The relevant error-causing widget was FutureBuilder

class PetDetail extends StatefulWidget { final int idproduct;

                          PetDetail({Key key, @required this.idproduct}) : super(key: key);
                        
                          @override
                          _PetDetailState createState() => _PetDetailState();
                        }
                        
                        class _PetDetailState extends State<PetDetail> {
                          // List data;
                          String token;
                        
                          @override
                          void initState() {
                            getproduct(widget.idproduct);
                            super.initState();
                          }
                        
                          Future<dynamic> getproduct(int id) async {
                            var response = await Network().getData('/publication/show/$id');
                            
                            var data = json.decode(response.body)['publication'];
                            
                            return data;
                          }
                        
                          @override
                          Widget build(BuildContext context) {
                            return Scaffold(
                              backgroundColor: Colors.white,
                              extendBodyBehindAppBar: true,
                              appBar: AppBar(
                                brightness: Brightness.light,
                                backgroundColor: Colors.transparent,
                                elevation: 0,
                                leading: GestureDetector(
                                  onTap: () {
                                    Navigator.pop(context);
                                  },
                                  child: Icon(
                                    Icons.arrow_back,
                                    color: Colors.grey[800],
                                  ),
                                ),
                              ),
                              body: Container(
                                padding: EdgeInsets.only(left: 16, top: 1, right: 16),
                                child: FutureBuilder(
                                    future: getproduct(widget.idproduct),
                                    builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
                                      return Column(
                                        crossAxisAlignment: CrossAxisAlignment.stretch,
                                        children: [
                                          Expanded(
                                            child: Stack(
                                              children: [
                                                Hero(
                                                  tag: Text(
                                                    "${snapshot.data['username']}",
                                                    style: TextStyle(
                                                      fontSize: 22.0,
                                                      color: Colors.grey,
                                                    ),
                                                  ),
                                                  child: Container(
                                                    decoration: BoxDecoration(
                                                      image: DecorationImage(
                                                        image: NetworkImage(
                                                          "${snapshot.data['picture1']}",
                                                          headers: {
                                                            "authorization": "Bearer $token",
                        
                                                            // Other headers if wanted
                                                          },
                                                        ),
                                                        fit: BoxFit.cover,
                                                      ),
                                                      borderRadius: BorderRadius.only(
                                                        bottomLeft: Radius.circular(25),
                                                        bottomRight: Radius.circular(25),
                                                      ),
                                                    ),
                                                  ),
                                                ),
                                              ],
                                            ),
                                          ),

if i inspect data from getproduct() . i got this

enter image description here

1
Inside getproduct, is response a list or a map?superhawk610
response is a listSEATECH Tunisie
You’re trying to do response.body[‘publication’], but you can’t index a list with a string. You either need to do response.body[0][‘publication’] or otherwise iterate through the list.superhawk610
if i inspect data from getproduct() . i got this result [0] : Map (24 items)SEATECH Tunisie

1 Answers

0
votes

Since this is very specific to your own code, and you did not give much info, I'm gonna take a wild guess and say check your getproduct(widget.idproduct), make sure you're passing in the right data type.