Home > Back-end >  Getting error "The getter document isn't defined for the type 'object'"
Getting error "The getter document isn't defined for the type 'object'"

Time:02-24

So I'm getting this error:

Try making the access conditional (using '?.') or adding a null check to the target ('!').

then when i add a ? there is a new error:

Try importing the library that defines 'documents', correcting the name to the name of an existing getter, or defining a getter or field named 'documents'.

I've already imported everything I need to idk why this is happening here is what is wrote:

import 'package:flutter/material.dart';
import 'package:messenger_complete_rebuild/services/auth.dart';
import 'package:messenger_complete_rebuild/services/database.dart';
import 'package:messenger_complete_rebuild/views/signin.dart';
import 'package:firebase_core/firebase_core.dart';

class Home extends StatefulWidget {
  const Home({Key? key}) : super(key: key);

  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  bool isSearching = false;
   late Stream usersStream;

  TextEditingController searchUsernameEditingController =
      TextEditingController();

  onSearchBtnClick() async {
    isSearching = true;
    setState(() {});
    usersStream = await DatabaseMethods()
        .getUserByUserName(searchUsernameEditingController.text);
        setState(() {});
  }

  Widget searchUsersList() {
    return StreamBuilder(
      stream: usersStream,
      builder: (context, snapshot) {
        return ListView.builder(
          itemCount: snapshot.data.documents.length,
          itemBuilder: (context, index) {
            DocumentSnapshot ds = snapshot.data.documents[index];
            return Image.network(ds['imgUrl']);
          },
        );
      },
    );
  }

I've been following a tutorial, it's one year old but the only issues so far have been of null safety, so this is weird, I have been following everything exactly, any help would be appreciated.

CodePudding user response:

Try snapshot.data!.docs instead of snapshot.data.documents

CodePudding user response:

Fixed it Replaced StreamBuilder with StreamBuilder<QuerySnapShot> and also added to late Stream usersStream (late Stream<QuerySnapshot> usersStream)

  • Related