firebase 当我试图打印和检查值时,将模型添加到List会一直给出该模型的示例

lskq00tm  于 2023-01-27  发布在  其他
关注(0)|答案(1)|浏览(86)

我正在从firestore获取数据并转换为一个模型,然后添加到该特定模型的项目列表中。我注意到有些不对劲,然后我试图检查该列表中项目的值,我发现它们都是该模型类的示例。附上终端的屏幕截图。
这是从Firestore检索数据的代码。

List<ItemModel> allProducts = [];

  Future<List<ItemModel>> getAllProducts() async {
    final QuerySnapshot snapshot =
        await FirebaseFirestore.instance.collection('Products').get();
    List<ItemModel> _allProducts = [];
    for (var doc in snapshot.docs) {
      print(doc['title']);
      _allProducts.add(ItemModel(
          description: doc['description'],
          id: doc['id'],
          imagepath: doc['imagepath'],
          price: doc['price'],
          title: doc['title'],
          amount: doc['amount']));
    }
    return _allProducts;
  }

  Future newList() async {
    print(allProducts);
    allProducts = await getAllProducts();
    print(allProducts);
  }

  @override
  void initState() {
    newList();
    super.initState();
  }

这是将文档快照从firebase转换为Item Model类的数据。

class ItemModel with ChangeNotifier {
  final String title;
  final int price;
  bool isCarted;
  final String id; // name of produce
  int amount; // amount uploader
  String description; // description of produce
  String imagepath; // image of produce
  bool isfavourited; // favorited produce

  ItemModel({
    required this.id,
    this.isCarted = false,
    required this.price,
    required this.title,
    this.amount = 1,
    required this.description,
    required this.imagepath,
    this.isfavourited = false,
  });

  factory ItemModel.fromMap(Map map) {
    return ItemModel(
      id: map['id'],
      isCarted: map['isCarted'],
      isfavourited: map['isfavourited'],
      amount: map['amount'],
      price: map['price'],
      title: map['title'],
      description: map['description'],
      imagepath: map['imagepath'],
    );
  }

  factory ItemModel.fromFirestore(
      DocumentSnapshot<Map<String, dynamic>> snapshot)  {
    final data = snapshot.data();
    return ItemModel(
      id: data!['id'],
      isCarted: data['isCarted'] as bool,
      isfavourited: data['isFavourited'] as bool,
      amount: data['amount'] as int,
      price: data['price'] as int,
      title: data['title'],
      description: data['description'],
      imagepath: data['imagepath'],
    );
  }

上图显示了控制台上的print语句。打印这些快照值本身会显示有效文本,如图所示。

rsaldnfx

rsaldnfx1#

当您尝试在控制台中打印List的项目对象时,它将始终返回您在控制台中接收到的输出。要打印单个值,您可以执行以下操作:

for (ItemModel product in allProducts) {
  // print all data members using product.<data-member>
  print(product.title);
  print(product.price);
}

或者,您可以在类中定义一个toString()方法覆盖,它始终显示对象的String输出。

class ItemModel with ChangeNotifier {
  final String title;
  final int price;
  bool isCarted;
  final String id; // name of produce
  int amount; // amount uploader
  String description; // description of produce
  String imagepath; // image of produce
  bool isfavourited; // favorited produce

  ItemModel({
    required this.id,
    this.isCarted = false,
    required this.price,
    required this.title,
    this.amount = 1,
    required this.description,
    required this.imagepath,
    this.isfavourited = false,
  });

  /// toString() override
  @override
  public String toString() {
    return 'title: $title, price: $price'; // you can add more values to be printed
  }

  factory ItemModel.fromMap(Map map) {
    return ItemModel(
      id: map['id'],
      isCarted: map['isCarted'],
      isfavourited: map['isfavourited'],
      amount: map['amount'],
      price: map['price'],
      title: map['title'],
      description: map['description'],
      imagepath: map['imagepath'],
    );
  }

  factory ItemModel.fromFirestore(
      DocumentSnapshot<Map<String, dynamic>> snapshot)  {
    final data = snapshot.data();
    return ItemModel(
      id: data!['id'],
      isCarted: data['isCarted'] as bool,
      isfavourited: data['isFavourited'] as bool,
      amount: data['amount'] as int,
      price: data['price'] as int,
      title: data['title'],
      description: data['description'],
      imagepath: data['imagepath'],
    );
  }

相关问题