skip to Main Content

I need to retrieve data from firestore collection and assign exact value to String…

Here is my code.

class _AbcState extends State<Abc> {
  @override
  Widget build(BuildContext context) {
    return StreamBuilder<QuerySnapshot>(
      stream: FirebaseFirestore.instance.collection('boat').snapshots(),
      builder: (context, snapshot) {
        if (snapshot.hasError) {
          return const Center(child: Text('Something wrong!'));
        }

        if (snapshot.connectionState == ConnectionState.waiting) {
          return const Center(child: CircularProgressIndicator());
        }

        if (snapshot.data!.size == 0) {
          return const Center(
            child: Text('No boats'),
          );
        }

        return ListView.builder(
          padding: const EdgeInsets.all(15.0),
          physics: const ScrollPhysics(),
          shrinkWrap: true,
          itemCount: snapshot.data!.size,
          itemBuilder: (context, index) {
            Map<String, dynamic> boatData =
                snapshot.data!.docs[index].data() as Map<String, dynamic>;

            return Card(
              child: ListTile(
                contentPadding: const EdgeInsets.all(15.0),
                horizontalTitleGap: 50,
                title: Text(boatData['boatName']),
                subtitle: GetInfo(boatData: boatData),
              ),
            );
          },
        );
      },
    );
  }
}

class GetInfo extends StatelessWidget {
  const GetInfo({
    Key? key,
    required this.boatData,
  }) : super(key: key);

  final Map<String, dynamic> boatData;

  @override
  Widget build(BuildContext context) {
    return StreamBuilder<DocumentSnapshot>(
      stream: FirebaseFirestore.instance
          .collection('actor')
          .doc(boatData['uid'])
          .snapshots(),
      builder: (context, snapshot) {
        if (snapshot.hasError) {
          return const Text('Something went wrong');
        }

        if (snapshot.connectionState == ConnectionState.waiting) {
          return const CircularProgressIndicator();
        }

        return Text(snapshot.data!['name']);
      },
    );
  }
}

By calling GetInfo I can get the data as a Text Widget.

But I need to get that value and assign it to a variable for future purposes.

I saved documents by user id in actor collection.

Please guide me to how to do that.

2

Answers


  1. one you can fix this is by having a function triggered for you in the _AbcState whenever there’s a valid name in GetInfo class.

    class GetInfo extends StatelessWidget {
    const GetInfo({Key? key, required this.boatData, required this.uidCallback})
      : super(key: key);
    
      final Map<String, dynamic> boatData;
      final Function(String?) uidCallback;
    
      @override
      Widget build(BuildContext context) {
       return StreamBuilder<DocumentSnapshot>(
      stream: FirebaseFirestore.instance
          .collection('actor')
          .doc(boatData['uid'])
          .snapshots(),
      builder: (context, snapshot) {
        if (snapshot.hasError) {
          return const Text('Something went wrong');
        }
    
        if (snapshot.connectionState == ConnectionState.waiting) {
          return const CircularProgressIndicator();
         }
         final _name = snapshot.data!['name'];
         uidCallback(_name);
         return Text(_name);
        },
       );
     }
    }
    

    then on the listview you returning when there’s data do this…

          return Card(
              child: ListTile(
                contentPadding: const EdgeInsets.all(15.0),
                horizontalTitleGap: 50,
                title: Text(boatData['boatName']),
                subtitle: GetInfo(
                  boatData: boatData,
                  uidCallback: (value) => setState(() {
                    _variable = value;
                  }),
                ),
              ),
            );
    

    so with this, whenever u get a valid name, the function in your _AbcState will be triggered with the corresponding name and you can do anything with the data.

    Login or Signup to reply.
  2. Because you are calling DocumentSnapshot in the StreamBuidlder

    Get the values like below

    snapshot.data!.get('uid')
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search