Home > Enterprise >  Error Expected a value of type 'Map<String, dynamic>', but got one of type '()
Error Expected a value of type 'Map<String, dynamic>', but got one of type '()

Time:01-06

today I got this error: Expected a value of type 'Map<String, dynamic>', but got one of type '() => Map<String, dynamic>?' The relevant error-causing widget was: StreamBuilder<DocumentSnapshot<Object?>> StreamBuilder:file:[Filepath] which is this widget:

              return StreamBuilder<DocumentSnapshot>(

                stream: database.getTodos(),

                builder: (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
                  if (!snapshot.hasData) {
                    return Center(child: CircularProgressIndicator());
                  }
                  else {
                    // resolve stream... Stream<DocumentSnapshot> -> DocumentSnapshot -> Map<String, bool>
                    Map<String, dynamic> items = snapshot.data?.data as Map<String, dynamic>;

                    return ListView.builder(
                        itemCount: items.length,
                        itemBuilder: (context, i) {
                          String key = items.keys.elementAt(i);
                          return ToDoItem(
                            key,
                            items[key],
                                () => deleteItem(key),
                                () => toggleDone(key, items[key]),
                          );
                        });
                  }
                },
              );

the error is causing from the Firestore Database, but I don´t know how to fix it. If you need something more to slove it, please comment and I will give you the code for it. I tried to slove it with adding some ?around or change the type Map oh and I also changed

  Stream getTodos() {
    return userTodos.document(userID).snapshots();
  }

in the database class to

  Stream<DocumentSnapshot> getTodos() {
    return userTodos.doc(userID).snapshots();
  }

but it doesn´t fix this error but another error. Thanks for help

CodePudding user response:

Here you wrote snapshot.data?.data but the second data is a function that returns a map, not a map itself, so you need to call it: snapshot.data?.data()

  •  Tags:  
  • Related