Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Flutter function that returns conditional does not give correct result

I have a code like this:

  //...
  child: ListTile(
    leading: CircleAvatar(
      backgroundImage: NetworkImage("${snapshot.data!.docs[index].data()['urunFotografi']}"),
    ),
    title: Text(snapshot.data!.docs[index].data()["urunAdi"], style: TextStyle(fontSize: 20),),
    subtitle: Text(adetVeyaKilo().toString(), style: TextStyle(fontSize: 15),),
  ),
  // ...

// ...
Future<String> adetVeyaKilo() async {
  String state = "";
    FirebaseFirestore.instance.collection('bolatAktar').where('urunAdi', isEqualTo: urunAdi).get().then((value) {
    value.docs.forEach((element) {
      if (element.data()["urunBirimi"] == "Adet") {
        state = "Adet";
      }
      if (element.data()["urunBirimi"] == "Kilo") {
        state = "Kilo";
      }
    });
  });
  return state;
}
// ...

With the codes I wrote, I am trying to make a return according to the data coming from Firebase Firestore.

But when trying to print to subTitle it prints like this: Instance of Future<String>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

Why does this problem occur? How can I solve it?

Thanks in advance for your help.

>Solution :

Try this:

Future<String> adetVeyaKilo() async {
  String state = "";
  await FirebaseFirestore.instance.collection('bolatAktar').where('urunAdi', isEqualTo: urunAdi).get().then((value) {
    value.docs.forEach((element) {
      if (element.data()["urunBirimi"] == "Adet") {
        setState((){
          state = "Adet";
        });
      }
      if (element.data()["urunBirimi"] == "Kilo") {
        setState((){
          state = "Kilo";
        });
      }
    });
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading