2

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(),
      ),
    );
  }
}
1

2 Answers 2

2

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());
      },
    );
  }
}
0
1

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();
                  }
                }
              }),
        ));
  }
}
1
  • Any time :),,,,
    – Shalabyer
    Commented Oct 24, 2020 at 7:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.