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
getproduct
, isresponse
a list or a map? – superhawk610response.body[‘publication’]
, but you can’t index a list with a string. You either need to doresponse.body[0][‘publication’]
or otherwise iterate through the list. – superhawk610