【问题标题】:The operator '[]' isn't defined for the type 'Object'.Try defining the operator '[]'没有为类型“Object”定义运算符“[]”。尝试定义运算符“[]”
【发布时间】:2021-09-14 08:30:33
【问题描述】:
    @override
    Widget build(BuildContext context) {
     final id = ModalRoute.of(context)!.settings.arguments;

     fetchPhotos() async {
      var res = await http.get(Uri.parse("path"));
      if (res.statusCode == 200) {
       var obj = json.decode(res.body);
       return obj;
         } else {
      return null;
     }
    }

   return Scaffold(
     appBar: AppBar(
      title: Text(
        'Products',
       ),
     ),
      body: Center(
         child: FutureBuilder(
           future: fetchPhotos(),
           builder: (ctx, snapShot) {
            if (snapShot.connectionState == ConnectionState.waiting) {
              return CircularProgressIndicator();
                  } else {
                return snapShot.data!["table"] == null ? Center(
                  child: Text(
                    "No Data",
                  ),
                )
              : ListView.builder(
                  itemCount: snapShot.data!["table"].length,
                  itemBuilder: (context, index) {
                    return ListTile(
                      leading: CircleAvatar(
                        backgroundColor: Colors.red,
                      ),
                      title: Text(snapShot.data!["table"][index]["name"]),
                      subtitle: Text(
                          "Price : ${snapShot.data!["table"][index]["price"]} ILS"),
                      trailing: Text(
                          "Class_id : ${snapShot.data!["table"][index]["class_id"]}"),
                    );
                  },
                );
              }
           },
         ),
       ),
     );
     }

在 snapShot.data!["table"][index]["name"] "table" => 没有为类型 'Object' 定义运算符 '[]'。尝试定义运算符 '[] ' / 在它没有给出任何错误并且运行良好之前,看起来这是从更新中发生的

【问题讨论】:

    标签: flutter


    【解决方案1】:

    每次使用[index] 的地方都可以转换吗?像这样:

    (snapShot.data!["table"] as List)[index]
    

    【讨论】:

      猜你喜欢
      • 2021-04-03
      • 1970-01-01
      • 2021-05-11
      • 1970-01-01
      • 2021-06-11
      • 2021-10-07
      • 1970-01-01
      • 1970-01-01
      • 2020-10-10
      相关资源
      最近更新 更多