Home > Net >  Firestore <QuerySnapshot>
Firestore <QuerySnapshot>

Time:11-20

I have a problem with my class GetInfo. There is an error with .

The name 'QuerySnapshot' is defined in the libraries 'package:cloud_firestore/cloud_firestore.dart' and 'package:firebase/src/firestore.dart (via package:firebase/firestore.dart)'. Try using 'as prefix' for one of the import directives, or hiding the name from all but one of the imports.

class GetInfo extends StatelessWidget {
  const GetInfo({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Material(
      child: StreamBuilder<QuerySnapshot>(
          stream: FirebaseFirestore.instance.collection('stories').snapshots(),
          builder: (context, snapshot) {
            if (snapshot.hasError) {
              return Text('Something went wrong');
            }
            if (snapshot.connectionState == ConnectionState.waiting) {
              return Text('Loading');
            }
            return new ListView(
              children: snapshot.data!.docs.map((document) {
                return new ListTile(
                  title: Text(document.get('display_name')),
                  subtitle: Text(document.get('profession')),
                );
              }).toList(),
            );
          }),
    );
  }
}

I am using Flatter with Firebase, Firestore. I am trying to follow one of the courses but it seems this is based on old Firebase Version and I don't know how to fix the code. Any advice? Thanks for your answers!

CodePudding user response:

the package is updated please check the example.

 body: StreamBuilder<QuerySnapshot<Movie>>(
        stream: moviesRef.queryBy(query).snapshots(),
        builder: (context, snapshot) {
          if (snapshot.hasError) {
            return Center(
              child: Text(snapshot.error.toString()),
            );
          }

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

          final data = snapshot.requireData;

          return ListView.builder(
            itemCount: data.size,
            itemBuilder: (context, index) {
              return _MovieItem(
                data.docs[index].data(),
                data.docs[index].reference,
              );
            },
          );
        },
      ),

final moviesRef = FirebaseFirestore.instance
    .collection('firestore-example-app')
    .withConverter<Movie>(
      fromFirestore: (snapshots, _) => Movie.fromJson(snapshots.data()!),
      toFirestore: (movie, _) => movie.toJson(),
    );

CodePudding user response:

I was able to fix this by simply hide this QuerySnapshot from my import.....

Unfortunately, I'm still stuck because I got another one. The problem is with

FirebaseFirestore.instance.collection('stories').snapshots(),

And the error message:

The argument type 'Stream<QuerySnapshot<Map<String, dynamic>>>' can't be assigned to the parameter type 'Stream?'.

Any advice?

  • Related