1

I am trying to work out how to pass data from stream to widget to display out elements

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';

class BrowseTasks extends StatefulWidget {
  static final String id = 'feed_screen';
  final String currentUserId;

  BrowseTasks({this.currentUserId});

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

class _BrowseTasksState extends State<BrowseTasks> {
  final Stream<QuerySnapshot> _usersStream =
      FirebaseFirestore.instance.collection('tasks').snapshots();

  Widget _buildTask(data) {
    return GestureDetector(
        onTap: () {},
        child: Card(
          clipBehavior: Clip.antiAlias,
          child: Column(
            children: [
              ListTile(
                leading: CircleAvatar(
                  backgroundColor: Color.fromRGBO(108, 212, 196, 1),
                  // backgroundImage: _displayProfileImage(),
                  child: Text(
                    'P',
                    style: TextStyle(
                      fontSize: 25.0,
                      fontWeight: FontWeight.w600,
                    ),
                  ),
                  foregroundColor: Colors.white,
                ),
                title: Text(
                    data.title,
                    maxLines: 2,
                    overflow: TextOverflow.ellipsis),
                trailing:
                    Text(data.budget, style: Theme.of(context).textTheme.headline5),

              ),
              Divider(),
              ListTile(
                leading: Text(data.owner),
                trailing: Icon(Icons.location_on_outlined),
              ),

              ButtonBar(
                alignment: MainAxisAlignment.start,
                children: [
                  TextButton(
                    // textColor: const Color(0xFF6200EE),
                    onPressed: () {
                      // Perform some action
                    },
                    child: const Text('ACTION 1'),
                  ),
                  TextButton(
                    // textColor: const Color(0xFF6200EE
                    // ),
                    onPressed: () {
                      // Perform some action
                    },
                    child: const Text('ACTION 2'),
                  ),
                ],
              ),
              // Image.asset('assets/card-sample-image.jpg'),
            ],
          ),
        ));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('Browse task'),
          bottom: PreferredSize(
              child: Container(
                color: Theme.of(context).primaryColor,
                height: 3.0,
              ),
              preferredSize: Size.fromHeight(4.0)),
          elevation: 0,
        ),
        body: StreamBuilder<QuerySnapshot>(
            stream: _usersStream,
            builder: (context, snapshot) {
              print('Has error: ${snapshot.hasError}');
              print('Has data: ${snapshot.hasData}');
              print('Snapshot Data ${snapshot.data.docs}');
              print('Connection State ${snapshot.connectionState}');
              if (snapshot.hasError) print(snapshot.error);

              if (snapshot.connectionState == ConnectionState.waiting) {
                return Center(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      CircularProgressIndicator(
                          valueColor:
                              new AlwaysStoppedAnimation<Color>(Colors.blue)),
                      // Loader Animation Widget
                      Padding(padding: const EdgeInsets.only(top: 20.0)),
                      Text('Finding tasks'),
                    ],
                  ),
                );
              }

              if (snapshot.hasData) {
                final documents = snapshot.data.docs;
                return ListView(
                    children: documents
                        .map(
                          (doc) => _buildTask(doc),
                        )
                        .toList());
              } else if (snapshot.hasError) {
                return Text('It\'s Error!');
              }
              return Center(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Text('Unable to  find any tasks'),
                  ],
                ),
              );
            }));
  }
}
1
  • Hi, aren't already doing that? Is there an error somewhere in your code? Commented Aug 7, 2021 at 17:43

1 Answer 1

1

try this if you have field name called "name" in task collection

class BrowseTasks extends StatefulWidget {
static final String id = 'feed_screen';
final String currentUserId;

BrowseTasks({this.currentUserId});

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

class _BrowseTasksState extends State<BrowseTasks> {
final Stream<QuerySnapshot> _usersStream =
FirebaseFirestore.instance.collection('tasks').get();

Widget _buildTask(data) {
    return GestureDetector(
    onTap: () {},
    child: Card(
      clipBehavior: Clip.antiAlias,
      child: Column(
        children: [
          ListTile(
            leading: CircleAvatar(
              backgroundColor: Color.fromRGBO(108, 212, 196, 1),
              // backgroundImage: _displayProfileImage(),
              child: Text(
                'P',
                style: TextStyle(
                  fontSize: 25.0,
                  fontWeight: FontWeight.w600,
                ),
              ),
              foregroundColor: Colors.white,
            ),
            title: Text(
                data.title,
                maxLines: 2,
                overflow: TextOverflow.ellipsis),
            trailing:
                Text(data.budget, style:         Theme.of(context).textTheme.headline5),

          ),
          Divider(),
          ListTile(
            leading: Text(data.owner),
            trailing: Icon(Icons.location_on_outlined),
          ),

          ButtonBar(
            alignment: MainAxisAlignment.start,
            children: [
              TextButton(
                // textColor: const Color(0xFF6200EE),
                onPressed: () {
                  // Perform some action
                },
                child: const Text('ACTION 1'),
              ),
              TextButton(
                // textColor: const Color(0xFF6200EE
                // ),
                onPressed: () {
                  // Perform some action
                },
                child: const Text('ACTION 2'),
              ),
            ],
          ),
          // Image.asset('assets/card-sample-image.jpg'),
        ],
      ),
    ));
}

@override
Widget build(BuildContext context) {
return Scaffold(
    appBar: AppBar(
      title: Text('Browse task'),
      bottom: PreferredSize(
          child: Container(
            color: Theme.of(context).primaryColor,
            height: 3.0,
          ),
          preferredSize: Size.fromHeight(4.0)),
      elevation: 0,
    ),
    body: StreamBuilder<QuerySnapshot>(
        stream: _usersStream,
        builder: (context, snapshot) {
          print('Has error: ${snapshot.hasError}');
          print('Has data: ${snapshot.hasData}');
          print('Snapshot Data ${snapshot.data.docs}');
          print('Connection State ${snapshot.connectionState}');
          if (snapshot.hasError) print(snapshot.error);

          if (snapshot.connectionState == ConnectionState.waiting) {
            return Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  CircularProgressIndicator(
                      valueColor:
                          new AlwaysStoppedAnimation<Color>(Colors.blue)),
                  // Loader Animation Widget
                  Padding(padding: const EdgeInsets.only(top: 20.0)),
                  Text('Finding tasks'),
                ],
              ),
            );
          }

          if (snapshot.hasData) {
           
            return ListView(
                children: snapshot.data.docs.map((document){
          Map<String, dynamic> data = document.data() as Map<String,   dynamic>;
                    ListTile(title:Text(data['name'])

                    ).toList());}
          } else if (snapshot.hasError) {
            return Text('It\'s Error!');
          }
          return Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text('Unable to  find any tasks'),
              ],
            ),
          );
        }));
     }
  }
Sign up to request clarification or add additional context in comments.

1 Comment

this got me partway there. I made some changes but this helped me greatly

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.