为什么它总是返回循环progressindicator


Widget buildForm() {
return FutureBuilder(
future: usernameCheck(context,this.username),
builder: (BuildContext context, AsyncSnapshot<bool> snapshot)  {
if (!snapshot.hasData) { // not loaded

return  CircularProgressIndicator();
}
else if (snapshot.hasError) {
return ErrorWidget(context);
}
else  {
bool valid = snapshot.hasData;
Future<bool> valid1;
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();
setState(() {
valid1=usernameCheck(context,_userNameController.toString());
});
},
validator: (value) {
if (value.isEmpty || valid1==false) {
setState(() {
onNextButtonClick = null;
});
}
else {
setState(() {
onNextButtonClick=next();
this.username = value;
this.usernameinfo.addAll(widget.previousFields);
this.usernameinfo.add(value);
}
);
return null;
}
return null;
},
controller: _userNameController,
autocorrect: false,
autofocus: false,
decoration: InputDecoration(
hintText: '@username',
hintStyle: TextStyle(
fontSize: 20,
),
border: InputBorder.none,
focusedBorder: InputBorder.none,
enabledBorder: InputBorder.none,
errorBorder: InputBorder.none,
disabledBorder: InputBorder.none,
),
keyboardType: TextInputType.text,
style: TextStyle(
fontSize: 20,
color: Colors.black,
fontWeight: FontWeight.w400,
),
),
),
);
}
}
);
}
}
Future<bool> usernameCheck(BuildContext context,String username)
async {

if(username!=null) {
final QuerySnapshot result = await FirebaseFirestore.instance
.collection('Consumers').where('Username', isEqualTo: username).get();
//converts results to a list of documents
final List<DocumentSnapshot> documents = result.docs;
//checks the length of the document to see if its
//greater than 0 which means the username has already been taken
if (documents.length > 0) {
//print(username + ' is already taken choose  another name');
showDialog(
context: context,
builder: (context) =>
new AlertDialog(
title: new Text('Taken'),
content: Text(username + ' is already taken choose  another name'),
actions: <Widget>[
new ElevatedButton(
onPressed: () {
Navigator.of(context, rootNavigator: true)
.pop(); // dismisses only the dialog and returns nothing
},
child: new Text('OK'),
),
],
),
);
return false;
}
else {
return true;
}
}
}

未来必须更早获得,例如在状态期间。initState,状态。didUpdateWidget或State.didChangeDependencies。它不得在国家期间创建。build或StatelessWidget。构建方法调用

您可以使用snapshop ConnectionState。

如果快照。= connectionState .done)

返回CircularProgressIndicator ();

最新更新