0

I always get this error when I use the String "name" to locate my Firestore docs. I don't understand why this is happening bc when I use "user.uid" it just works.

The instance member 'name' can't be accessed in an initializer. Try replacing the reference to the instance member with a different expression

This is my code:

final user = FirebaseAuth.instance.currentUser!;

class Folder extends StatelessWidget {
  Folder(this.name, {super.key});

  final String name;

  final Stream<QuerySnapshot> items = FirebaseFirestore.instance
      .collection('users')
      .doc(user.uid)
      .collection('Folder')
      .doc(name)
      .collection('Items')
      .snapshots();

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        // Title
        Text(name, style: Theme.of(context).textTheme.headlineSmall),
        // List with Items
        StreamBuilder(
          stream: items,
          builder:
              (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
            final data = snapshot.requireData;

            if (snapshot.hasData) {
              return ListView.builder(
                itemCount: data.size,
                shrinkWrap: true,
                physics: const NeverScrollableScrollPhysics(),
                itemBuilder: (context, index) {
                  return ListTile(
                    title: Text(data.docs[index]['name']),
                    onTap: () {},
                  );
                },
              );
            }

            if (snapshot.hasError) {
              return const Text("error");
            }

            return const Text("error");
          },
        ),
      ],
    );
  }
}

And I pass the String from this StreamBuilder:

          stream: folder,
          builder:
              (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
            final data = snapshot.requireData;

            if (snapshot.hasError) {
              return const Center(child: CircularProgressIndicator());
            }

            if (snapshot.hasData) {
              return Padding(
                padding: const EdgeInsets.all(00.0),
                child: ListView.builder(
                  padding: const EdgeInsets.only(bottom: 100, top: 20),
                  itemCount: data.size,
                  itemBuilder: (context, index) {
                    return Folder(
                      data.docs[index]["name"].toString()
                    );
                  },
                ),
              );
            }

            return const Center(child: CircularProgressIndicator());
          },
        ),
Lorenz
  • 1

1 Answers1

0

You could just add a late modifier to the items variable. Like this:

  late final Stream<QuerySnapshot> items = FirebaseFirestore.instance
      .collection('users')
      .doc(user.uid)
      .collection('Folder')
      .doc(name)
      .collection('Items')
      .snapshots();

To explain why this works: basically you can't use any instance variables when initializing other instance variables because they are all initialized when the class gets instantiated. When you add the late modifier, the variable will only be initialized when it's first used, meaning all the non-late variables will already be initialized.

Luccas Clezar
  • 1,032
  • 10
  • 18