无法将元素类型“Future<Widget>”分配给列表类型“Widget”解决此错误的给定解决方案均不适合我

问题描述

我尝试了几种解决方案,但对我没有任何作用
//这基本上布局了屏幕的结构 @覆盖 小部件构建(BuildContext 上下文){ 返回脚手架( 应用栏:应用栏(), 主体:容器( 对齐:Alignment.center, 填充:const EdgeInsets.only( 顶部:30, 底部:60, ),孩子:列( 孩子们: [ 构建标题(), 大小盒( 身高:50, ),构建形式(), 间隔(), 构建底部(), ],),); }

//problem is with buildForm
Future<Widget> buildForm() async {
    final valid = await usernameCheck(this.username);

     return Container(
      width: 330,decoration: Boxdecoration(
    color: Colors.white,borderRadius: BorderRadius.circular(8),child: Form(
    key: _userNameformKey,child: TextFormField(
      textAlign: TextAlign.center,onChanged: (value) {
        _userNameformKey.currentState.validate();
      },validator: (value) {
        if (value.isEmpty ) {
          setState(() {
            onNextButtonClick = null;

          });
        }
        else if(!valid){
          setState(() {
            //user.user.username=value;

            onNextButtonClick = null;
            showDialog(
              context: context,builder: (context) =>
              new AlertDialog(
                title: new Text('Status'),content: Text(
                    'Username already taken'),actions: <Widget>[
                  new ElevatedButton(
                    onpressed: () {
                      Navigator.of(context,rootNavigator: true)
                          .pop(); // dismisses only the dialog and returns nothing
                    },child: new Text('OK'),],);

解决方法

尝试使用 FutureBuilder<T>

Widget build(_) {
  return FutureBuilder<bool>(
    future: usernameCheck(this.username),builder: (BuildContext context,AsyncSnapshot<bool> snapshot) {
      if(!snapshot.hasData) { // not loaded
        return const CircularProgressIndicator();
      } else if(snapshot.hasError) { // some error
        return const ErrorWidget(); // create this class
      } else { // loaded
        bool valid = snapshot.data;
        return Container(/*...details omitted for conciseness...*/);
      }
    }
  )
}