0

I migrated my code to null safety but now I get an issue: When I want to fetch a document from firestore I get this error:

Error: The operator '[]' isn't defined for the class 'Object'.

Here is the code snippet where I want to call ['plans'] on the snapshot data.

body: FutureBuilder(
            future: handler.getPlans(),
            builder: (context, snapshot) {
              if (snapshot.hasData) {
                final plans = snapshot.data!['plans']; /// here I cant call ['plans']

Before null safety this worked. Whats the issue?

Dalon
  • 566
  • 8
  • 26
  • 1
    Does this answer your question? [The operator '\[\]' isn't defined for the type 'Object'. Try defining the operator '\[\]'](https://stackoverflow.com/questions/66086647/the-operator-isnt-defined-for-the-type-object-try-defining-the-operator) – tbs Oct 07 '21 at 11:26
  • No without the '!' i get this error: The method '[]' can't be unconditionally invoked because the receiver can be 'null'. – Dalon Oct 07 '21 at 11:30
  • I think its not about the '!'. The answers are about the type of the FutureBuilder (FutureBuilder) – tbs Oct 07 '21 at 11:33
  • Thank you this was the issue! I also needed to add a '?' – Dalon Oct 07 '21 at 11:39
  • You are welcome. Where exactly do u want to add the '?'? At the type? – tbs Oct 07 '21 at 11:43
  • 1
    I needed to add 'FutureBuilder.' I think because my future handler.getPlans() can catch an error – Dalon Oct 07 '21 at 11:46

0 Answers0