【问题标题】:Flutter: error void is not a subtype of typeFlutter:错误 void 不是类型的子类型
【发布时间】:2021-10-20 11:50:29
【问题描述】:

请帮忙。我有 categoryListItems 功能来构建列表视图。

ListView categoryListItems() {
    return ListView.builder(
      shrinkWrap: true,
      padding: EdgeInsets.all(0),
      itemCount: this.count,
      itemBuilder: (BuildContext context, int index) {
        return ListTile(
          dense: true,
          title: Text(
            this.cats[index].title,
            style: TextStyle(
              fontWeight: FontWeight.bold,
              fontSize: 16,
            ),
          ),
          onTap: () => openAddCategoryDialog(this.cats[index]),
        );
      },
    );
  }

我也有 openAddCategoryDialog 函数,为 onTap 方法提供参数

    void openAddCategoryDialog(Category cat) async {
    Category save = await Navigator.of(context).push(
      new MaterialPageRoute<Category>(
        builder: (BuildContext context) {
          return new AddCategoryScreen(cat);
        },
        fullscreenDialog: true,
      ),
    );
    if (save != null) {
      setState(() {
        getData();
      });
    }
  }

一旦调试我得到以下错误。 在构建 LayoutScreen(dirty, state: _LayoutScreenState#35628) 时引发了以下 _TypeError: type '(Category) => void' 不是类型 '(() => void) 的子类型?

【问题讨论】:

  • 尝试删除提示框功能的 void 关键字
  • 该错误表明您有一个将Category 作为必需的位置参数的函数,并且您将该函数传递给预期不带参数的(可选)函数。但是,我在您显示的代码中看不到这一点。您确定错误来自此代码而不是其他地方吗?
  • 我刚刚从函数 openAddCategoryDialog 中删除了 void 类型,它工作正常。谢谢!

标签: flutter dart


【解决方案1】:

当您使用 Category save = ... 时,意味着它永远不会返回null。 push 返回可为空的 Future 对象。

尝试类别?保存 = 等待 Navigator.of(context).push(..

演示


class Body extends StatefulWidget {
  const Body({Key? key}) : super(key: key);

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

class _BodyState extends State<Body> {
  void openAddCategoryDialog(int cat) async {
    Category? save = await Navigator.of(context).push(
      new MaterialPageRoute<Category>(
        builder: (BuildContext context) {
          return Scaffold(
            body: Container(
              child: Text("value got $cat"),
            ),
            floatingActionButton: FloatingActionButton(
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
          );
        },
        fullscreenDialog: true,
      ),
    );
    if (save != null) {
      print(" got value ");
    } else
      print("got null");
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: LayoutBuilder(
        builder: (context, constraints) => ListView.builder(
          shrinkWrap: true,
          padding: EdgeInsets.all(0),
          itemCount: 4,
          itemBuilder: (BuildContext context, int index) {
            return ListTile(
              dense: true,
              title: Text("title here"),
              onTap: () => openAddCategoryDialog(Random().nextInt(44)),
            );
          },
        ),
      ),
    );
  }
}

【讨论】:

    猜你喜欢
    • 2023-03-14
    • 2021-02-06
    • 1970-01-01
    • 2022-07-10
    • 2021-07-13
    • 2020-08-07
    • 2021-02-17
    • 1970-01-01
    • 2022-01-20
    相关资源
    最近更新 更多