having this error :type 'String' is not a subtype of type 'int' of 'index' on flutter app

having this error :type 'String' is not a subtype of type 'int' of 'index' on flutter app

我正在尝试从 strapi 获取数据以显示在我的 flutter 应用程序上。它显示 'String' 类型不是 'index'.

类型 'int' 的子类型

我想在此 product_page.dart 组件上展示产品:

import 'package:flutter/material.dart';
import 'package:flutter_ecommerce/models/app_state.dart';
import 'package:flutter_ecommerce/widgets/product_item.dart';
import 'package:flutter_redux/flutter_redux.dart';



class ProductsPage extends StatefulWidget {
  final void Function() onInit;
  ProductsPage({this.onInit});

  @override
  ProductsPageState createState() => ProductsPageState();
}

class ProductsPageState extends State<ProductsPage> {
  void initState() {
    super.initState();
    widget.onInit();
  }

  

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: _appBar,
        body: Container(
            decoration: gradientBackground,
            child: StoreConnector<AppState, AppState>(
                converter: (store) => store.state,
                builder: (_, state) {
                  return Column(children: [
                    Expanded(
                        child: SafeArea(
                            top: false,
                            bottom: false,
                            child: GridView.builder(
                                itemCount: state.products.length,
                                gridDelegate:
                                    SliverGridDelegateWithFixedCrossAxisCount(
                                        crossAxisCount: 2),
                                itemBuilder: (context, i) =>
                                    ProductItem(item: state.products[i]))))
                  ]);
                })));
  }
}

在我试图显示每个项目的这个组件上。 Product_item.dart:

import 'package:flutter/material.dart';


class ProductItem extends StatelessWidget {
  final dynamic item;
  ProductItem({this.item});



  @override
  Widget build(BuildContext context) {
    final String pictureUrl = 'http://localhost:1337${item['picture']['url']}';
    return GridTile(child: Image.network(pictureUrl, fit: BoxFit.cover));
  }
}

当您尝试使用接受整数作为数组索引的字符串时,会发生此错误。

因此,从您的代码来看,问题可能来自“item['picture']['url']”

该数组将改为接受类似“item[0]”的某些东西......或者无论如何构建 array/data。您应该检查数组是如何设置的。

如果您不确定,我会先尝试打印出要调试的数组,而不是首先将其发送到 ProductItem 小部件。

所以可能像下面这样:

child: GridView.builder(
                                itemCount: state.products.length,
                                gridDelegate:
                                    SliverGridDelegateWithFixedCrossAxisCount(
                                        crossAxisCount: 2),
                                itemBuilder: (context, i) =>
                                   {
                                        Print(state.products[i]);
                                        Print(state.products[i][0]);
                                   }
                  ]);