0

I'am making a user profile in flutter. I wanted to display the data from my database that I made in Firebase. I get an error in my text saying that the operator [] isn't defined for the type QuerySnapshot. How can I resolve this. Thank you

class _ProfileState extends State<Profile> {
  final Stream<QuerySnapshot> users =
  FirebaseFirestore.instance.collection('users').snapshots();
@override
Widget build(BuildContext context) {
return Scaffold(
    body: SizedBox(
        child: Column(
            children: [
              Expanded(
                  child: Column(
                children: [
                  
                  StreamBuilder<QuerySnapshot>(
                    stream: users,
                    builder: (
                      BuildContext context,
                      AsyncSnapshot<QuerySnapshot> snapshot,
                    ) {
                        if (snapshot.hasError) {
                        var data = 'Something went wrong.!';
                        return Text(data);
                      }
                      if (snapshot.connectionState ==
                          ConnectionState.waiting) {
                        return const Text ('loading');
                      }
                                  Column(children: const [
                                    Text(
                                      '${data['name']}',
                                    ),
                                   Text(
                                      '${data['age']}',
                                    ),
                                  ])
                    },
  )
                ],
              )),
            ])));
}}
2
  • where is stream users in StreamBuilder? Commented Jul 13, 2022 at 5:26
  • final Stream<QuerySnapshot> users = FirebaseFirestore.instance.collection('users').snapshots(); --- sorry I misstyped that part Commented Jul 13, 2022 at 7:47

2 Answers 2

2

Try this:

StreamBuilder<QuerySnapshot>(
              stream: FirebaseFirestore.instance
                  .collection(AppConfig.instance.cUser)
                  .snapshots(),
              builder: (BuildContext context,
                  AsyncSnapshot<QuerySnapshot> chatSnapshot) {
                if (chatSnapshot.connectionState == ConnectionState.waiting) {
                  return Center(
                    child: Container(),
                  );
                }
                return ListView(
                  reverse: true,
                  controller: _controller,
                  physics: const BouncingScrollPhysics(),
                  children:
                      chatSnapshot.data!.docs.map((DocumentSnapshot document) {
                    Map<String, dynamic> data =
                        document.data()! as Map<String, dynamic>;
                    return Text(data['text']);
                  }).toList(),
                );
              },
            ),
Sign up to request clarification or add additional context in comments.

3 Comments

I got an error saying undefined name for AppConfig and _controller
_controller is ScrollController, remove it if you don't need, and myAppConfig.instance.cUser is name for your collection in firebase, change it
I got another error which states that 'Null' is not a subtype of type String. Do I need to assign a value somewhere?
0

Use hasData to check that snapshot contains a non-null value and data to access it.

1 Comment

I tried using hasData and it appears that my snapshot has a null value. what could have probably gone wrong?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.