【问题标题】:How to edit properties of a Custom Model in a list (Flutter/Dart)?如何在列表中编辑自定义模型的属性(Flutter/Dart)?
【发布时间】:2020-03-06 05:25:24
【问题描述】:

我的颤振应用程序中有这个日志食物功能。有两种添加食物的方法 (1) 是添加到列表中,这相当容易,因为我所要做的就是 listName.add(ModelName(properties: value); 但是,(2) 方法是编辑此处显示的初始列表:

import 'package:thesis/foodAPICalls/food.dart';

class MealsListData {
  int id;
  String imagePath;
  String titleTxt;
  String startColor;
  String endColor;
  DateTime date;
  Nutritionix nutritionix;
  String meals;
  double totalCalories;

  MealsListData({
    this.id,
    this.imagePath = '',
    this.titleTxt = '',
    this.startColor = "",
    this.endColor = "",
    this.date,
    this.nutritionix,
    this.meals,
    this.totalCalories = 0,
  });

  static List<MealsListData> mealEntry = [
     MealsListData(
      id: 1,
      imagePath: 'assets/fitness_app/breakfast.png',
      titleTxt: 'Breakfast',
      totalCalories:  0,
      meals: '',
      date: DateTime.now(),
      startColor: "#FA7D82",
      endColor: "#FFB295",
    ),
    MealsListData(
      id: 2,
      imagePath: 'assets/fitness_app/lunch.png',
      titleTxt: 'Lunch',
      totalCalories: 0,
      meals: '',
      date: DateTime.now(),
      startColor: "#738AE6",
      endColor: "#5C5EDD",
    ),
    MealsListData(
      id: 3,
      imagePath: 'assets/fitness_app/snack.png',
      titleTxt: 'Snack',
      totalCalories: 0,
      meals: 'Recommend: 800 kcal',
      startColor: "#FE95B6",
      endColor: "#FF5287",
    ),
    MealsListData(
      id: 4,
      imagePath: 'assets/fitness_app/dinner.png',
      titleTxt: 'Dinner',
      totalCalories: 0,
      meals: 'Recommend: 703 kcal',
      startColor: "#6F72CA",
      endColor: "#1E1466",
    ),

  ];
}

这是 UI-Wise 的样子:

我在 UI 中创建了一个函数,它将列表的 ID 传递到我的 addFood() 屏幕

onTap:(){
 Navigator.push( context,MaterialPageRoute
(builder: (context) => AddFood(id: mealsListData.id,)),);
},

在添加食物屏幕中,我创建了一个冗长的嵌套 if-else 来检查用户是否尝试使用方式 1 或方式 2 添加

 void _submitData() {
  List<MealsListData> mealEntry = MealsListData.mealEntry;
    if (_formKey.currentState.validate()) {
      _formKey.currentState.save();
      double totalCalories = 0;
      print(nutritionix.food.length);
      for (int i = 0; i < nutritionix.food.length; i++) {
        totalCalories = nutritionix.food[i].calories + totalCalories;
      }
      print(totalCalories);

      if (idcount == null) { // WAY 1 (adding to initial List)
        switch (_foodTypeInput) {
          case 'Breakfast':
            {
              mealEntry.add(MealsListData(
                id: idcount++,
                imagePath: 'assets/fitness_app/breakfast.png',
                titleTxt: _foodTypeInput,
                totalCalories: totalCalories,
                meals: _food,
                date: _selectedDate,
                nutritionix: nutritionix,
                startColor: "#FA7D82",
                endColor: "#FFB295",
              ));
              Navigator.pop(context);
            }
            break;

          case 'Lunch':
            {
              mealEntry.add(MealsListData(
                id: idcount++,
                imagePath: 'assets/fitness_app/lunch.png',
                titleTxt: _foodTypeInput,
                totalCalories: 590,
                meals: _food,
                date: _selectedDate,
                nutritionix: nutritionix,
                startColor: "#738AE6",
                endColor: "#5C5EDD",
              ));
              Navigator.pop(context);
            }
            break;

          case 'Snack':
            {
              mealEntry.add(MealsListData(
                id: idcount++,
                imagePath: 'assets/fitness_app/snack.png',
                titleTxt: _foodTypeInput,
                totalCalories: 200,
                meals: _food,
                date: _selectedDate,
                nutritionix: nutritionix,
                startColor: "#FE95B6",
                endColor: "#FF5287",
              ));
              Navigator.pop(context);
            }
            break;

          case 'Dinner':
            {
              mealEntry.add(MealsListData(
                id: idcount++,
                imagePath: 'assets/fitness_app/dinner.png',
                titleTxt: _foodTypeInput,
                totalCalories: 600,
                meals: _food,
                date: _selectedDate,
                nutritionix: nutritionix,
                startColor: "#6F72CA",
                endColor: "#1E1466",
              ));
              Navigator.pop(context);
            }
            break;
        }
      } else{ // WAY 2 editing index based on id
        var newEntry = mealEntry.firstWhere((id) => id.id == idcount, orElse: null );
        if(newEntry!=null){
        newEntry.totalCalories = totalCalories;
        newEntry.meals= _food;
        newEntry.nutritionix= nutritionix;
        Navigator.pop(context);
        } else{}

      }

      print(mealEntry);
      print(_foodTypeInput);
      print(_food);
      print(_selectedDate);
    }
  }

对于方式 2:我所做的是为我的自定义模型创建了 firstWhere,但是我不断收到此错误“Bad state: No element”我该如何解决这个问题?感谢您阅读我的长篇文章。感谢您的帮助!

【问题讨论】:

    标签: arrays list flutter dart


    【解决方案1】:

    糟糕,原来我没有使用传递给小部件的数据。我应该使用 widget.idcount 而不仅仅是 idcount。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-11
      • 1970-01-01
      • 2020-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多