Cant check if user account exists?

Viewed 163

Helo, I am trying to check if user's account exists, if no, I want to run text 'account is deleted'.

But the problem is that when I start the app there is screen for existing account and only after reset I can get the real result.

Looks like check for account is done after running app for the first time, but I don't know where is the mistake.

Here is the code, thank you in advance:

class CheckIfDeletedAccount extends StatelessWidget {
  String isAccountDeleted;

  getData() async {
    var userType = await Firestore.instance
        .collection('users')
        .where('userEmail', isEqualTo: email)
        .getDocuments();
    userType.documents.forEach((result) {
      log(result.data["deleted"]);
      isAccountDeleted = result.data["deleted"].toString();
    });
  }

  @override
  Widget build(BuildContext context) {
    getData();
    //log(isAccountDeleted);
    if (isAccountDeleted == "true") {
      return Scaffold(
        body: Container(
          child: Center(
            child: Text("account is deleted"),
          ),
        ),
      );
    }
    return MaterialApp(
      theme: themeData,
      home: Scaffold(
        body: Bar(),
      ),
    );
  }
}
2 Answers

You need to wait for the result from Firebase. You are trying to build the widget before the isAccountDeleted is initialized.

In your scenario, you can use FutureBuilder as follows:

class CheckIfDeletedAccount extends StatelessWidget {
  String isAccountDeleted;

  Future<String> getData() async {
    var userType = await Firestore.instance
        .collection('users')
        .where('userEmail', isEqualTo: email)
        .getDocuments();
    userType.documents.forEach((result) {
      log(result.data["deleted"]);
      isAccountDeleted = result.data["deleted"].toString();
    });
    return isAccountDeleted;
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
    future: getData(),
    builder: (BuildContext context, AsyncSnapshot snapshot) {
    if(snapshot.connectionState == ConnectionState.done &&
         snapshot.hasData) {
        final isAccountDeleted = snapshot.data;

        if (isAccountDeleted == "true") {
          return Scaffold(
            body: Container(
              child: Center(
                child: Text("account is deleted"),
              ),
            ),
          );
        }

        return MaterialApp(
          theme: themeData,
          home: Scaffold(
            body: Bar(),
          ),
        );
    }
      return Center(child: const CircularProgressIndicator());
      },
    );
  }
}

Based on savke comment you can use the following code using FutureBuilder:

import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';

class CheckIfDeletedAccount extends StatelessWidget {

  Future getData() async {
    String  isAccountDeleted;
    var userType = await Firestore.instance
        .collection('users')
        .where('userEmail', isEqualTo: email)
        .getDocuments();
    userType.documents.forEach((result) {
      log(result.data["deleted"]);
      isAccountDeleted = result.data["deleted"].toString();
    });
    return isAccountDeleted;
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        theme: themeData,
        home: Scaffold(
          body: FutureBuilder(
              future: getData(),
              builder: (context, AsyncSnapshot snapshot) {
                if (!snapshot.hasData) {
                  return Center(
                    child: CircularProgressIndicator(
                      strokeWidth: 6,
                      valueColor: AlwaysStoppedAnimation<Color>(Colors.red),
                    ),
                  );
                } else {
                  if (snapshot.data == "true") {
                      return  Container(
                        child: Center(
                          child: Text("account is deleted"),
                        ),
                      );
                    }
                   else {
                    return Bar();
                  }
                }
              }),
        ));
  }
}
Related