【问题标题】:How can I create a list view from the data of the list inside a list?如何从列表中的列表数据创建列表视图?
【发布时间】:2021-12-23 06:32:08
【问题描述】:

错误:“参数类型 'Breakfast' 不能分配给参数类型 String。dart argument_type_not_assignable 早餐配料”。我不知道如何查看列表中的列表。

   class BreakfastIngredient extends StatefulWidget {
      const BreakfastIngredient({
        Key? key,
      required this.breakfast,
      }) : super(key: key);

      final Breakfast breakfast;

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

   class _BreakfastIngredientState extends State<BreakfastIngredient> {
   
   late List ingredients;

   @override

   Widget build (BuildContext context) {
    return Row(
      children: breakfastProducts.map((ingredients){
        return  Text(ingredients);
        }
         ).toList()
     );  
    }
   }

'我正在尝试从此列表中获取数据'。我想从“配料”中获取数据。

    List<Breakfast> breakfastProducts = [
     Breakfast(
      id: 1,
      images: [
        "assets/images/cilantro.png",
      ],
      title: "Cilantro and Kale Pesto Toast with a Fried Egg",
        time: 15,
        description: "Sliced bread is the perfect blank canvas, ready to be loaded up with virtuous ingredients.",
    rating: 4.8,
   isFavourite: true,
   isPopular: true,
   ingredients: [
        "¼ cup packed cilantro",
      "1 cup packed kale leaves",
       "¼ cup extra-virgin olive oil",
     "1 tablespoon white balsamic vinegar",
      "2 tablespoons hulled hemp seeds*",
      "salt",
        "Freshly ground pepper",
       "1 large slice of whole-wheat toast",
      "2 tablespoons unflavored whole-milk Greek yogurt",
       "1 fried egg",
   ],
   procedure: "procedure",
  ),
];

【问题讨论】:

    标签: flutter flutter-listview


    【解决方案1】:

    问题是 Text 小部件只接受一个字符串作为参数,而不是一个列表,一种可能的解决方法是将所有成分连接到一个字符串中:

       return Row(
          children: breakfastProducts.map((ingredients){
            return  Text(ingredients.join('\n'));
    //Join every ingredient and insert a new line between each other
            }
             ).toList()
         );  
    

    更高效的方法是使用 ListView 懒惰地创建小部件,但这取决于您想要实现的目标:

    
       ListView.builder(
    itemCount: breakfastProducts.ingredients.length   
    ,itemBuilder: (_,i)=>Text(breakfastProducts.ingredients[i])),
         
    

    完整示例: 请注意,您在这里使用的是单个早餐对象

       class BreakfastIngredient extends StatefulWidget {
          const BreakfastIngredient({
            Key? key,
          required this.breakfast,
          }) : super(key: key);
    
          final Breakfast breakfast;
    
          @override
         _BreakfastIngredientState createState() => _BreakfastIngredientState();
       }
    
       class _BreakfastIngredientState extends State<BreakfastIngredient> {
       
       late List ingredients;
    
       @override
    
       Widget build (BuildContext context) {
    final ingredients=widget.breakfast.ingredients;
    return Text(ingredients.join("\n"));
       }
    

    【讨论】:

    • 错误:没有为类型 'List' 定义 getter 'ingredients'。
    • 不知道为什么无法访问
    • 如果是图像,那里没有问题,但是当字符串列表时,我无法获取
    • 我添加了一个完整的示例,但小部件将单个 Breakfast 项目作为参数,您似乎很难显示“列表”及其相应的成分。是这样吗?您想将配料列表与所有早餐显示在同一页面中,或者与 ExpansionTilo 一起显示,或者可能是一个详细信息页面?
    • 您好,几行代码就解决了。
    猜你喜欢
    • 2020-08-24
    • 2020-05-13
    • 1970-01-01
    • 2022-11-28
    • 1970-01-01
    • 2022-09-22
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    相关资源
    最近更新 更多