0
votes

I have collection where i want to get fields. I have followed instructions on flutter fire and it says to use get() to retrieve documentsnapshot. I am creating a documentsnapshot object and trying to get all for a given id. Now i am trying to pass this stream into streambuilder and display specific fields. But i am getting error;

type '_BroadcastStream<DocumentSnapshot<Map<String, dynamic>>>' is not a subtype of 
type
'Stream<QuerySnapshot<Object?>>?'

enter image description here

My code is;

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/cupertino.dart';

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:quicktodo/model/authservice.dart';

class events extends StatefulWidget {
analytics({Key? key}) : super(key: key);
String uida = FirebaseAuth.instance.currentUser!.uid;

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

class _ eventsState extends State<analytics> {

 late final _stream;
String uid = FirebaseAuth.instance.currentUser!.uid;

void initState() {
super.initState();


setState(() {
  _stream =   FirebaseFirestore.instance.collection('events').doc(uid).get();
});
}




@override
Widget build(BuildContext context) {

final authService = Provider.of<AuthClass>(context);
return Scaffold(
  appBar:AppBar(
    leading:
    IconButton(
      icon: Icon(Icons.logout),
      tooltip: 'List of your activities',
      onPressed: () {
        authService.signout(
        );
      },
    ),

    title: const Text('Activity list'),
    actions: <Widget>[
      IconButton(
        icon: const Icon(Icons.add),
        tooltip: 'List of your activities',
        onPressed: () {
        },
      ),
    ],
  ),
  body: StreamBuilder(
    stream: _stream,
    builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
      if (snapshot.hasError) {
        return Text('Something went wrong');
      }

      if (snapshot.connectionState == ConnectionState.waiting) {
        return Text("Loading");
      }

      return ListView(
        children: snapshot.data!.docs.map((DocumentSnapshot document) {
          // final trip = DataModel.fromSnapshot(document);
          Map a = document.data() as Map<String, dynamic>;

          a['id'] = document.id;
          return Container(
            child: Text(a['eventone']),
          );
        }).toList(),
      );


    },

  ),

);

}

}

1

1 Answers

0
votes

Replace your stream builder with this:

return StreamBuilder(
  stream: FirebaseFirestore.instance
      .collection('events')
      .doc(uid)
      .snapshots(),
  builder:
      (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
    if (snapshot.hasError) return Text('Something went wrong');
    if (snapshot.connectionState == ConnectionState.waiting)
      return Text("Loading");

    Map data = snapshot.data.data();
    print(data['eventide']); // should print 4
    print(data['eventtwo']); // should print 5
    return SizedBox();
  },
);

I changed your initial stream builder because it will only work for collection snapshot, not document snapshot.