0
votes

I tried couple of solutions given but nothing worked for me
//this baically laysout the structure of the screen @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(), body: Container( alignment: Alignment.center, padding: const EdgeInsets.only( top: 30, bottom: 60, ), child: Column( children: [ buildTitle(), SizedBox( height: 50, ), buildForm(), Spacer(), buildBottom(), ], ), ), ); }

//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'),
                  ),
                ],
              ),
            );
1

1 Answers

0
votes

Try using 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...*/);
      }
    }
  )
}