有没有一种方法来获取嵌套对象的特定属性

时间:2020-04-05 07:24:33

标签: flutter dart

我正在使用Flutter开发食品食谱应用程序,正在将Firestore用于数据库,即从Firestore获取食谱和配料。我已经通过创建一个模型类实现了“成分”页面,该模型类将基于预定义的属性和对象将firebase对象转换为地图。我正在尝试对Recipes模型使用相同的方法,Recipes模型对象除其他属性外还包含Ingredients模型,即List<Ingredients> _ings = List<Ingredients>(); 但是,当我尝试根据配方模型中的嵌套模型成分对象在配方屏幕中调用配方列表中的属性时,会带来错误The getter 'name' isn't defined for the class 'List'. Try importing the library that defines 'name',这可能是错误的,这是我正在使用的代码获取配料模型属性,`

db.getRecipesList().listen((QuerySnapshot snapshot) {
      final List<RecipesModel> recs = snapshot.documents
          .map((documentSnapshot) => RecipesModel.fromMap(documentSnapshot.data))
          .toList();
      setState(() {
        this.items = recs.where((item) => widget.ing.toString().toLowerCase()
        .contains(item.ingredients.name.toString().toLowerCase()));
      });
    });

为什么食谱列表看不到嵌套成分对象的属性。以下是RecipesModelIngredientsModelRecipesScreen的代码。

IngredientsModel.dart

class Ingredients {
  String _name;
  String _image;

  Ingredients(this._name, this._image);

  Ingredients.map(dynamic obj) {
    this._name = obj['name'];
    this._image = obj['icon'];
  }

  String get name => _name;
  String get image => _image;

  Map<String, dynamic> toMap() {
    var map = new Map<String, dynamic>();
    map['name'] = _name;
    map['icon'] = _image;

    return map;
  }

  Ingredients.fromMap(Map<String, dynamic> map) {
    this._name = map['name'];
    this._image = map['icon'];
  }
}

RecipesModel.dart

import 'package:ffo/models/ingredients.dart';
class RecipesModel {
  String _name;
  String _steps;
  List<Ingredients> _ings = List<Ingredients>();
  String _image;

  RecipesModel(this._name, this._image, this._steps, this._ings);

  RecipesModel.map(dynamic obj) {
    this._name = obj['name'];
    this._steps = obj['steps'];
    this._ings = obj['ingredients'];
    this._image = obj['image'];
  }

  String get name => _name;
  String get image => _image;
  String get steps => _steps;
  List get ingredients => _ings;

  Map<String, dynamic> toMap() {
    var map = new Map<String, dynamic>();
    map['name'] = _name;
    map['image'] = _image;
    map['steps'] = _steps;
    map['ingredients'] = _ings.map((attribute) {
  Map<String,dynamic> attributeMap = new Map<String,dynamic>();
  attributeMap["name"] = attribute.name;
  attributeMap["icon"] = attribute.image;
  // same for xpos and ypos
  return attributeMap;
}).toList();
    return map;
  }

  RecipesModel.fromMap(Map<String, dynamic> map) {
    this._name = map['name'];
    this._image = map['image'];
    this._ings = map['ingredients'];
    this._steps = map['steps'];
  }
}

RecipesScreen.dart

import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:ffo/helpers/firebase.dart';
import 'package:ffo/models/recipes.dart';

import 'package:flutter/services.dart';
import 'dart:async';
class Recipes extends StatefulWidget {
  final List<String> ing; //list of ingredients in string form to search through the recipes for
    Recipes({Key key, this.title, @required this.ing}) : super(key: key);
  final String title;

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

class _RecipesState extends State<Recipes> {
  List<RecipesModel> items;
  FirebaseFirestoreService db = new FirebaseFirestoreService();
  StreamSubscription<QuerySnapshot> ingredientsSub;
    @override
  void initState() {
    super.initState();
    items = new List();
    ingredientsSub?.cancel();
    ingredientsSub = db.getRecipesList().listen((QuerySnapshot snapshot) {
      final List<RecipesModel> recs = snapshot.documents
          .map((documentSnapshot) => RecipesModel.fromMap(documentSnapshot.data))
          .toList();
      setState(() {
        this.items = recs.where((item) => widget.ing.toString().toLowerCase()
        .contains(item.ingredients.name.toString().toLowerCase()));/* at this point it says "The getter 'name' isn't defined for the class 'List'.
Try importing the library that defines 'name', correcting the name to the name of an existing getter, or defining a getter or field named 'name'"*/
      });
    });
  }

  @override
  void dispose() {
    ingredientsSub?.cancel();
    super.dispose();
  }

0 个答案:

没有答案
相关问题