0

So I have a flutter program which is reading values from Cloud Firestore and stores them into variables. I can normally show their value with Text(), or change them. Now I wanted to make a Color display with a simple if statement. So if the Firestore value for example is bigger than 1 (>1), the color shall be blue. If it is smaller, then red.

But for some reason, when opening the page, it gives me this error: The method '>=' was called on null. Receiver: null Tried calling: >=(0.0)

But if I simply remove my if code, start the program again, and then add it again, it works just fine. It seems that it has a problem with the start values, but how do I fix that?

                            stream: FirebaseFirestore.instance
                                .collection('Digitaluhr')
                                .snapshots(),
                            builder: (context,
                                AsyncSnapshot<QuerySnapshot> snapshot) {
                              int itemCount =
                                  snapshot.data.docs.length; //Dokumentlänge
                              itemCount--; //-1 weil Index
                              Luftguete = snapshot.data.docs[itemCount]
                                  ['Luftgüte']; //Luftgüte Wert zum Verarbeiten
                              return ListTile(

if (Luftguete > 2.00 && Luftguete <= 3.00)
                              Container(
                                decoration: BoxDecoration(
                                  shape: BoxShape.circle,
                                  border: Border.all(
                                      width: 40, color: Colors.yellow),
                                ),
                                child: Text('Mäßig',
                                    style: TextStyle(
                                        height: 0.00001,
                                        color: Colors.white,
                                        backgroundColor: Colors.yellow)),
                              ),

                            if (Luftguete > 3.00 && Luftguete <= 4.00)
                              Container(
                                decoration: BoxDecoration(
                                  shape: BoxShape.circle,
                                  border: Border.all(
                                      width: 40, color: Colors.orange),
                                ),
                                child: Text('Schlecht',
                                    style: TextStyle(
                                        height: 0.00001,
                                        color: Colors.white,
                                        backgroundColor: Colors.orange)),
                              ),
Leon Brey
  • 41
  • 1
  • 1
  • 4

1 Answers1

0

Well, this is guesswork, because you did not actually post the code in question... but Luftguete seems to be not a local variable, but probably in your state. So... before your StreamBuilder (I guess, not in your code either) fires the first time, it is null. And you probably access it somewhere else. If you make Luftguete a local variable, you will find where else you access it. Because you will get compiler errors :)

Or, maybe you have a data point where snapshot.data.docs[itemCount]['Luftgüte'] is simply null. We don't know your data. You would need to check that.

See also: What is a NoSuchMethod error and how do I fix it?

nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • Hello, thanks for your quick response. Yes, you guessed right. Luftguete is a global variable which reads the value. But the thing is, if I remove my if commands, it shows me all data correctly and monitors it. But if I open the page with the if commands activated, the error occurs. I have to remove the ifs, press STRG+S, then place them again, again STRG+S and then I can see them. – Leon Brey Feb 10 '22 at 21:11