You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter新手求助:bool类型无法赋值给Bool参数的错误解决

错误解决:The argument type 'bool' can't be assigned to the parameter type 'Bool'

问题根源

你在QuestionA类里错误使用了FFI专用的布尔类型Bool,而非Dart原生的布尔类型bool:

  • 导入了完全不需要的dart:ffi库,这个库是用于和C语言交互的,你的Quiz应用根本用不上
  • 类的answers字段和构造函数参数都用了Bool类型,但创建QuestionA实例时传的是Dart原生的bool值(比如true),两者类型不兼容,直接引发报错

修复步骤

修改qaa.dart文件的代码:

  1. 删除无用的import 'dart:ffi';语句
  2. 把所有Bool替换成Dart原生的bool
  3. 可选:将构造函数改成更简洁的Dart初始化写法

修改后的QuestionA类代码:

class QuestionA {
  late String questions;
  late bool answers;

  QuestionA({required String q, required bool a}) {
    questions = q;
    answers = a;
  }

  // 更简洁的构造写法(推荐)
  // QuestionA({required this.questions, required this.answers});
}

关于你提到的「从Future获取布尔值」

如果之后需要处理异步返回的Future<bool>,可以用async/await语法实现同步取值:

// 示例异步函数
Future<bool> getAsyncBool() async {
  // 模拟异步操作,比如网络请求
  await Future.delayed(const Duration(seconds: 1));
  return true;
}

// 使用时
void handleAsyncBool() async {
  bool result = await getAsyncBool();
  // 这里拿到了同步的bool值,可直接使用
  print(result);
}

内容的提问来源于stack exchange,提问作者Angad Singh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 10:39:50