错误:无法将类型为"未来"的值<Widget>分配给 'Widget' 颤振类型的变量



当我想显示此方法生成的qrcode时,会出现此错误。我必须生成这个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,
),
),
),
],
);
});
}
}

喜欢吗(建议简单编辑(

///Define A state Variable
bool isQrImgReady = false;
Future<Widget> buildQrCode() async {
Map<String,dynamic> myData = {
'type': 1,
'content': name,
'mac' : await Utilities.getDeviceMac(),
};
String encodedJson = jsonEncode(myData);
QrCodeEncrypter.encrypt(password, myData);
///Change state variable to True when its ready
setState((){
isQrImgReady = true;
});
return QrImage(
data: encodedJson,
size: 320,
gapless: false,
);

然后

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,
),
),
),
///Check if Widget is ready or not
this.isQrImgReady 
? buildQrCode() 
: SizedBox(),
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 ; 
}); 
}

然后在堆栈中用qrCodeHolder交换builQrCode。

相关内容

  • 没有找到相关文章

最新更新