【问题标题】:The operator '[]' isn't defined for the type 'Object Function()'未为类型“Object Function()”定义运算符“[]”
【发布时间】:2022-01-05 16:02:48
【问题描述】:

你好... 至少在我的两个页面上,我几乎遇到了这个问题,我做了很多来解决它,但似乎我做不到。 这是我在产品页面中的代码:

    import 'package:cloud_firestore/cloud_firestore.dart';

class ProductModel {
  static const ID = "id";
  static const NAME = "name";
  static const PICTURE = "picture";
  static const PRICE = "price";
  static const DESCRIPTION = "description";

  String _id;
  String _name;
  String _picture;
  String _description;
  

  String get id => _id;

  String get name => _name;

  String get picture => _picture;

  String get brand => _brand;

  String get category => _category;

  String get description => _description;

  ProductModel.fromSnapshot(DocumentSnapshot snapshot) {
    _id = snapshot.data[ID];
    _brand = snapshot.data[BRAND];
    _sale = snapshot.data[SALE];
    _description = snapshot.data[DESCRIPTION] ?? " ";
   

  }
}

这里给出了错误

The operator '[]' isn't defined for the type 'Object Function()'.

指的是模型...

这里是 pubspec.yaml

environment:
  sdk: ">=2.1.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter
  carousel_pro: any
  google_sign_in: ^5.2.1
  firebase_storage: ^10.1.0
  cloud_firestore: ^3.1.0
  firebase_core: ^1.10.0
  firebase_auth: ^3.2.0
  firebase_analytics: ^8.3.4
  shared_preferences: ^2.0.9
  image_picker: ^0.8.4+4
  firebase_database: ^8.1.0
  flutter_search_panel: any
  intl: ^0.17.0
  provider: ^6.0.1
  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^1.0.4
  carousel_slider: ^4.0.0
  transparent_image: ^2.0.0
  flutter_spinkit: ^5.1.0
  uuid: ^3.0.5
  flutter_typeahead: ^3.2.3

如果有任何帮助,我将不胜感激

非常感谢

【问题讨论】:

    标签: android flutter flutter-dependencies


    【解决方案1】:
    _id = snapshot.data[ID];
    _brand = snapshot.data[BRAND];
    _sale = snapshot.data[SALE];
    _description = snapshot.data[DESCRIPTION] ?? " ";
    

    snapshot.data 不是地图,它是一个返回地图的函数,所以我应该有括号才能调用它:

    _id = snapshot.data()[ID];
    _brand = snapshot.data()[BRAND];
    _sale = snapshot.data()[SALE];
    _description = snapshot.data()[DESCRIPTION] ?? " ";
    

    希望微小的变化足以解决问题

    【讨论】:

    • 感谢您的帮助,但它不会再次使 [BRAND] 成为订单,我的意思是它给出了这些错误
    • 只在品牌上?你能告诉我错误吗?这是否与您显示的代码中未声明 BRAND 的事实有关?
    • 不仅是品牌,当我将货架()放在[ID]之前或其他人本身(我的意思是[ID])之前都会出错。我希望你能明白,因为我的英语不太好。 _brand = snapshot.data()[品牌]; --------- 那条线是红色的
    • 好的,但是错误说明了什么?您能否复制粘贴错误消息或显示带有可见错误消息的屏幕截图?对不起
    • 好的,感谢您的帮助。这是图片的链接:ibb.co/tZjSfLf
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-23
    • 1970-01-01
    • 2021-04-03
    • 2021-09-10
    • 1970-01-01
    • 2021-11-30
    • 2021-06-11
    相关资源
    最近更新 更多