flutter - 错误:“未来”类型的值'不能分配给'Widget'类型的变量颤动
问题描述
当我想显示此方法生成的 qrcode 时出现此错误。我必须生成这个二维码,然后在屏幕上显示,但我发现编译错误。我来自 Swift,我不清楚如何构建视图
Future<Widget> buildQrCode() async {
Map<String,dynamic> myData = {
'type': 1,
'content': name,
'mac' : await Utilities.getDeviceMac(),
};
String encodedJson = jsonEncode(myData);
QrCodeEncrypter.encrypt(password, myData);
return QrImage(
data: encodedJson,
size: 320,
gapless: false,
);
}
这是我调用该方法以在屏幕上显示图像的地方:
@override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
_buttonHeight = size.height * .05;
double splitPoint = size.height / 7;
return FutureBuilder<UserData>(
future: contentManager.getUserData(),
builder: (context, AsyncSnapshot<UserData> snapUserData) {
if (snapUserData.hasError)
return Container(
child: Center(
child: Text("There was some error"),
),
);
if (snapUserData.connectionState != ConnectionState.done)
return Container(
child: Center(
child: CircularProgressIndicator(),
),
);
return Stack(
alignment: AlignmentDirectional.center,
children: [
// SFONDO
Positioned(
bottom: 0,
child: Container(
height: size.height,
width: size.width,
color: appColors.primaryColor,
),
),
// TITOLO
Positioned(
top: size.height * .05,
child: Text(
localization.showQR,
style: Theme.of(context).primaryTextTheme.headline5.copyWith(
color: appColors.green,
fontWeight: FontWeight.bold,
),
),
),
buildQrCode(),
Positioned(
bottom: size.height * .18,
width: size.width * .8,
child: AutoSizeText(
localization.home_subLabel,
textAlign: TextAlign.center,
maxLines: 1,
style: Theme.of(context).primaryTextTheme.headline5.copyWith(
color: appColors.green,
fontWeight: FontWeight.bold,
),
),
),
Positioned(
bottom: size.height * .08,
width: size.width * .7,
height: size.height * .05,
child: TextField(
inputFormatters: [
new LengthLimitingTextInputFormatter(11),
],
decoration: InputDecoration(
filled: true,
fillColor: Colors.white,
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.white),
),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.white),
),
),
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 18.0,
//height: 2.0,
color: Colors.black,
),
),
),
],
);
});
}
}
解决方案
这里的问题是您没有异步调用 buildQrCode,因为您没有使用 await 关键字,并且您不能使构建器函数异步。
因此,正确的方法是在 StatefulWidget 中声明一个小部件变量,该变量将最初使用 initState 保存 buildQrCode,然后将该小部件放入堆栈中。如下 :
Widget qrCodeHolder ;
void initState() {
super.initState() ;
setQrCodeHolder();
}
setQrCodeHolder() async {
var tmp = await buildQrCode() ;
setState (() {
qrCodeHolder = tmp ;
});
}
然后在您的堆栈中将您的 builQrCode 与 qrCodeHolder 交换。
推荐阅读
- spring - 如何在 Spring 4.1.7 中将模拟注入自定义 ConstraintValidator?
- encryption - RSA 在加密句子时如何工作?
- mysql - 显示订购的所有产品名称列表以及客户的名字和姓氏
- nginx - 如何通过入口注释将流量重定向到我的 Web 服务?
- git - Azure Devops 开发掌握合并问题
- node.js - 何时何地使用 Firebase admin.app().delete()?
- laravel - 从 Laravel 5.8 中的抽象类的静态方法发送通知?
- java - 为许多组件有效地存储布尔事件的简短历史
- java - 从 cmd 运行 mvn clean install 时出现 NoClassDefFoundError com/google/inject/Module
- sql - MS-Access-2016 查询语言“ORDER BY”子句