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文件的代码:
- 删除无用的
import 'dart:ffi';语句 - 把所有
Bool替换成Dart原生的bool - 可选:将构造函数改成更简洁的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
相关产品推荐
相关产品推荐

