1

Right now I have a Firestore folder which contains numbered documents with information like Volume, temperature, air quality. These values are saved as numbers. I somehow want these Firestore numbers to be saved as Integer variables in my flutter project, so that I can work with them (For example show dark colors if the Volume is too loud).

But it just doesn't work, no matter what I try. Is it even possible? I can only display the firestore values, but I can't save them..

Here a picture of my database: enter image description here

My code:

 Future<String> getValue() async{ final doc = await FirebaseFirestore.instance .collection('Digitaluhr') .doc('1.002') .get(); Future <String> lautstaerke = doc['Lautstärke']; return lautstaerke; } @override Widget build(BuildContext context) { Future <String> Lautstaerke = getValue(); String transform = Lautstaerke.toString(); var test = int.parse(transform); assert(test is int); Text('$test'), 

Output: enter image description here

Please help :(

3
  • 1
    Since getValue is a Future, you have to use async/await to get its value. If you need this in a build method, FutureBuilder can solve your problem. Commented Feb 2, 2022 at 12:10
  • Hello @LeonBrey, any progress? Please consider accepting and upvoting the answer that helped you. Thanks Commented Feb 3, 2022 at 9:41
  • Hello, thank you, but my intention is not the "get" the value by displaying it, my intention is to store the Firestore number into an integer, with which I can then work with. (For example with if commands) Commented Feb 8, 2022 at 9:19

3 Answers 3

1

Check this out:

@override Widget build(BuildContext context){ return FutureBuilder<String>( future: getValue(), builder: (_, snapshot){ if(snapshot.hasData){ var test = int.parse(snapshot.data); ... } return Container(); } ); } 
Sign up to request clarification or add additional context in comments.

9 Comments

It doesnt know "value".
Hey @LeonBrey...Use snapshot.data sorry - I've edited my answer.
Thanks. But it then still simply returns the Container (Error).
Is something wrong with my method maybe?
Do you want it to show the text?
|
1

Posting LeonBrey's solution for visibility.

The problem was fixed with the following:

Luftguete = snapshot.data.docs[itemCount] ['Luftgüte']; 

Comments

0

I think issue is doc['Lautstärke'] return string, while your variable data type is Future<String>

Future <String> lautstaerke = doc['Lautstärke']; 

Try changing like below

String lautstaerke = doc['Lautstärke']; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.