首页 > 解决方案 > 错误:“未来”类型的值'不能分配给'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,
                      ),
                  ),
              ),
            ],
          );
        });
  }
}

标签: flutter

解决方案


这里的问题是您没有异步调用 buildQrCode,因为您没有使用 await 关键字,并且您不能使构建器函数异步。

因此,正确的方法是在 StatefulWidget 中声明一个小部件变量,该变量将最初使用 initState 保存 buildQrCode,然后将该小部件放入堆栈中。如下 :

Widget qrCodeHolder ;

void initState() {
  super.initState() ;
  setQrCodeHolder();
}

setQrCodeHolder() async {
  var tmp = await buildQrCode() ; 
  setState (() {
    qrCodeHolder = tmp ; 
  }); 
}

然后在您的堆栈中将您的 builQrCode 与 qrCodeHolder 交换。


推荐阅读