当我想显示此方法生成的二维码时出现此错误。 我必须生成这个二维码,然后在屏幕上显示它,但我发现一个编译错误。我来自 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,
),
),
),
],
);
});
}
}
答案 0 :(得分:0)
喜欢吧(建议做一个简单的编辑)
///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,
),
),
),
],
);
});
}
}
答案 1 :(得分:0)
这里的问题是您没有异步调用 buildQrCode,因为您没有使用 await 关键字,并且您无法使构建器函数异步。
因此,正确的方法是在 StatefulWidget 中声明一个小部件变量,该变量最初将使用 initState 保存 buildQrCode,然后将该小部件放入您的堆栈中。如下:
Widget qrCodeHolder ;
void initState() {
super.initState() ;
setQrCodeHolder();
}
setQrCodeHolder() async {
var tmp = await buildQrCode() ;
setState (() {
qrCodeHolder = tmp ;
});
}
然后在您的堆栈中将您的 builQrCode 与 qrCodeHolder 交换。