Home > Blockchain >  How to save a Firestore number into a variable in flutter?
How to save a Firestore number into a variable in flutter?

Time:02-02

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 :(

CodePudding user response:

Check this out:

@override
Widget build(BuildContext context){
  return FutureBuilder<String>(
    future: getValue(),
    builder: (_, snapshot){
      if(snapshot.hasData){
        var test = int.parse(snapshot.value);
        ...
      }
      return Container();
    }
  );
}

CodePudding user response:

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'];
  •  Tags:  
  • Related