当我尝试在列表视图中使用时,我设置了 bool 条件,我收到了这个错误

i make bool condition when i try to use in listveiw i got this error

当我尝试在 listveiw 中使用时我设置了 bool 条件我得到了这个错误(预期有 1 个位置参数,但找到了 0 个。尝试添加缺少的参数。)当传递 isBool 值时它给了我这个错误(未定义的名称 'isBool'。 尝试将名称更正为已定义的名称,或定义名称。)

import 'package:chad_cafe/configs/color.dart';
import 'package:flutter/material.dart';

class SigleItem extends StatelessWidget {
  //const SigleItem({Key? key}) : super(key: key);

  bool isBool = false;
  SigleItem(this.isBool);
  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 10),
      child: Row(
        children: [
          Expanded(
              child: Container(
            child: Center(
              child: Image.asset('assets/bbqpizza.png'),
            ),
            height: 100,
          )),
          Expanded(
              child: Container(
            height: 100,
            child: Column(
              mainAxisAlignment: isBool == false
                  ? MainAxisAlignment.spaceAround
                  : MainAxisAlignment.spaceEvenly,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Column(
                  children: [
                    Text(
                      'ProductName',
                      style: TextStyle(
                          color: textColor, fontWeight: FontWeight.bold),
                    ),
                    const Text(
                      'RS-799',
                      style: TextStyle(
                        color: Colors.grey,
                      ),
                    )
                  ],
                ),
                isBool == false
                    ? Container(
                        margin: const EdgeInsets.only(right: 15),
                        padding: const EdgeInsets.symmetric(horizontal: 10),
                        height: 35,
                        decoration: BoxDecoration(
                          border: Border.all(color: Colors.grey),
                          borderRadius: BorderRadius.circular(30),
                        ),
                        child: Row(
                          children: [
                            const Expanded(
                              child: Text(
                                'RS-799',
                                style: TextStyle(
                                  color: Colors.grey,
                                ),
                              ),
                            ),
                            Center(
                              child: Icon(
                                Icons.arrow_drop_down,
                                size: 20,
                                color: primaryColor,
                              ),
                            ),
                          ],
                        ),
                      )
                    : const Text('750')
              ],
            ),
          )),
          Expanded(
            child: Container(
                height: 100,
                padding: isBool == false
                    ? EdgeInsets.symmetric(horizontal: 15, vertical: 32)
                    : EdgeInsets.only(left: 15, right: 15),
                child: isBool == false
                    ? Container(
                        decoration: BoxDecoration(
                          border: Border.all(color: Colors.grey),
                          borderRadius: BorderRadius.circular(30),
                        ),
                        child: Center(
                          child: Row(
                            children: [
                              Icon(
                                Icons.add,
                                color: primaryColor,
                                size: 20,
                              ),
                              Text(
                                'Add',
                                style: TextStyle(
                                  color: primaryColor,
                                ),
                              )
                            ],
                          ),
                        ),
                      )
                    : Column(
                        children: [
                          const Icon(
                            Icons.delete,
                            size: 30,
                            color: Colors.black,
                          ),
                          const SizedBox(height: 5),
                          Container(
                            decoration: BoxDecoration(
                              border: Border.all(color: Colors.grey),
                              borderRadius: BorderRadius.circular(30),
                            ),
                            child: Center(
                              child: Row(
                                children: [
                                  Icon(
                                    Icons.add,
                                    color: primaryColor,
                                    size: 20,
                                  ),
                                  Text(
                                    'Add',
                                    style: TextStyle(
                                      color: primaryColor,
                                    ),
                                  )
                                ],
                              ),
                            ),
                          )
                        ],
                      )),
          ),
          isBool == false
              ? Container()
              :const Divider(
                  height: 1,
                  color: Colors.black,
                ),
        ],
      ),
    );
    isBool == false
        ? Container()
        : Divider(
            height: 1,
            color: Colors.black,
          );
  }
}





import 'package:chad_cafe/Widgets/single_item.dart';
import 'package:chad_cafe/configs/color.dart';
import 'package:flutter/material.dart';

class ReviewCart extends StatelessWidget {
  const ReviewCart({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      bottomNavigationBar: ListTile(
        title: const Text("Total Amount"),
        trailing: Container(
          width: 160,
          child: MaterialButton(
            onPressed: () {},
            child: const Text('Submit '),
            color: primaryColor,
            shape:
                RoundedRectangleBorder(borderRadius: BorderRadius.circular(30)),
          ),
        ),
      ),
      appBar: AppBar(
        backgroundColor: primaryColor,
        title: const Text(
          'Review Cart',
          style: TextStyle(
            fontSize: 18,
          ),
        ),
      ),
      body: ListView(
        children: [
          const SizedBox(
            height: 10,
          ),
          SigleItem(isBool),//Undefined name 'isBool'.
Try correcting the name to one that is defined, or defining the name.
          const SizedBox(
            height: 10,
          ),
        ],
      ),
    );
  }
}

我认为你正在合并你的 class 和小部件 class,所以我最好的猜测是你应该将 bool 参数传递给你的构造函数;

 body: ListView(
        children: [
          const SizedBox(
            height: 10,
          ),
          SigleItem(isBool),
          const SizedBox(
            height: 10,
          ),
        ],
      ),

如果我猜错了,请在问题中添加更多信息

问题是变量 isBoolReviewCart 的构建方法的上下文中未定义 - 它仅作为 SingleItem 实例的成员可用。

当您构造 SingleItem 的新实例时,您需要向其传递一个值,该值可以是定义的变量或布尔文字:

class ReviewCart extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // omitted
      body: ListView(
        children: [
          SingleItem(false); // or true, or some variable which is defined in this context.
        ],
      ),
    );
  }
}

根据名称 ReviewCart 判断,在某些时候您可能会根据购物车中的产品列表构建列表 - 下面是一个非常简单的示例:

class ReviewCart extends StatelessWidget {
  List<Product> products;

  ReviewCart(this.products);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // omitted
      body: ListView(
        children: products.map((product) => 
          SingleItem(product.someBooleanValue)
        ).toList(),
      ),
    );
  }
}

'isBool' 个实例属于 'SingleItem' class。 在你的 'ReviewCart' class 中,你试图传递 'isBool'(它自己),它在这个 class 中不可用,同时创建 'SingleItem' 的实例class.

尝试传递实际的布尔值 (true/false) 而不是 'isBool'。