My StreamBuilder contains two for me unsolvable errors


I’m trying to build a to-do app. (To learn flutter)
This is the part of the code where the two errors are:

class ToDo1 extends StatefulWidget {
  _ToDo1State createState() => _ToDo1State();

class _ToDo1State extends State<ToDo1> {

  var User;
  late DatabaseService database;

  Future<void> connectToFirebase() async{
    await Firebase.initializeApp();
    final FirebaseAuth auth = FirebaseAuth.instance;
    UserCredential result = await FirebaseAuth.instance.signInAnonymously();
    User = result.user;
    database = DatabaseService(User.uid);

if (!(await database.checkIfUserExists())) {
      database.setTodo('To-Do anlegen', false);

  void toggleDone(String key, bool value) {
    database.setTodo(key, !value);

  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Center(
            child: Text(
          'Stufe 1',
          style: TextStyle(
              fontStyle: FontStyle.italic,
              decoration: TextDecoration.underline),
        backgroundColor: Color.fromRGBO(35, 112, 192, 1),
      body: FutureBuilder(
        future: connectToFirebase(),
            builder: (BuildContext context, AsyncSnapshot snapshot) {
          if (snapshot.connectionState == ConnectionState.waiting) {
            return Center(child: CircularProgressIndicator());
          } else {
            return StreamBuilder<DocumentSnapshot> (
              stream: database.getTodos(),
              builder: (context, AsyncSnapshot<DocumentSnapshot> snapshot) {
                if(!snapshot.hasData) {
                  return CircularProgressIndicator();
                } else {
                  Map<String, dynamic> items =;
                  return ListView.separated(
                      separatorBuilder: (BuildContext context, int index) {
                        return SizedBox(
                          height: 10,
                      padding: EdgeInsets.all(10),
                      itemCount: items.length,
                      itemBuilder: (BuildContext, i) {
                        String key = items.keys.elementAt(i);
                        return ToDoItem(
                              () => toggleDone(key, items[key]),

This is underlined in red:

Stream: database.getTodos()

and this:

This is the class that interacts with the Firebase:

class DatabaseService {
  final String userID;

  final CollectionReference userTodos =

  Future setTodo(String item, bool value) async {
    return await userTodos.doc(userID).set(
      {item:value}, SetOptions(merge: true));

    Future deleteTodo(String key) async {
      return await userTodos.doc(userID).update(
        {key: FieldValue.delete(),}

    Future checkIfUserExists() async {
    if((await userTodos.doc(userID).get()).exists) {
      return true;
      else {
        return false;

    Stream getTodos() {
    return userTodos.doc(userID).snapshots();

And finally, here’s the error:

Error: Property 'data' cannot be accessed on 'DocumentSnapshot<Object?>?' because it is potentially null.
 - 'DocumentSnapshot' is from 'package:cloud_firestore/cloud_firestore.dart' ('/C:/flutter/.pub->cache/hosted/').
 - 'Object' is from 'dart:core'.
Try accessing using ?. instead.
                  Map<String, dynamic> items =;
lib/Pages/Home_Page.dart:347:62: Error: A value of type 'Object? Function()' can't be assigned to a variable of type 'Map<String, dynamic>'.
 - 'Object' is from 'dart:core'.
 - 'Map' is from 'dart:core'.
                  Map<String, dynamic> items =;
lib/Pages/Home_Page.dart:342:32: Error: The argument type 'Stream<dynamic>' can't be assigned to the parameter type 'Stream<DocumentSnapshot<Object?>>?'.
 - 'Stream' is from 'dart:async'.
 - 'DocumentSnapshot' is from 'package:cloud_firestore/cloud_firestore.dart' ('/C:/flutter/.pub->cache/hosted/').
 - 'Object' is from 'dart:core'.
              stream: database.getTodos(),

FAILURE: Build failed with an exception.

* Where:
Script 'C:\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 1035

* What went wrong:
Execution failed for task ':app:compileFlutterBuildDebug'.
 Process 'command 'C:\flutter\bin\flutter.bat'' finished with non-zero exit value 1

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at

Exception: Gradle task assembleDebug failed with exit code 1

I hope I have provided all the necessary data so that the two problems can be solved.
If not, just write it to me and I will try to send you the material you need.


The first problem with Stream: database.getTodos() is that your getTodos() method needs a return type like so Stream<DocumentSnapshot<Object?>>? getTodos().

The second problem with is that can be null, and you can’t access something that may eventually be null. However since you are already checking it in !snapshot.hasData, this seems like a false alarm, thus you can remove the nullability of it by assigning the value!.data, the ! tells the compiler that at this point can’t have a null value.

Answered By – esentis

Answer Checked By – Marilyn (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *