0

can anybody show me how to replace the static list (_dataFromQuerySnapShot) with Firestore Firebase QuerySnapshot? Thank you! `

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

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

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Column(
        children: <Widget>[
          const TextField(onChanged: _filter),
          StreamBuilder<List<User>>(
            stream: _stream,
            builder: (context, snapshot) {
              return StreamBuilder<List<User>>(
                key: ValueKey(snapshot.data),
                initialData: snapshot.data,
                stream: _stream,
                builder: (context, snapshot) {
                  return ListView.builder(
                    shrinkWrap: true,
                    itemCount: snapshot.data?.length,
                    itemBuilder: (BuildContext context, int index) {
                      final data = snapshot.data?[index]?.name;
                      return Text(data!);
                    },
                  );
                },
              );
            },
          )
        ],
      ),
    );
  }
}

StreamController<List<MyUser>> _streamController = StreamController<List<MyUser>>();
Stream<List<MyUser>> get _stream => _streamController.stream;
_filter(String searchQuery) async {
  List<MyUser> _dataFromQuerySnapShot = await getData();
  List<MyUser> _filteredList = _dataFromQuerySnapShot.where((MyUser user) => user.firstName!.toLowerCase().contains(searchQuery.toLowerCase())).toList();
  _streamController.sink.add(_filteredList);
}

Future<List<MyUser>> getData() async {
  List<MyUser> dataList = [];
  CollectionReference myRef = FirebaseFirestore.instance.collection('users');
  QuerySnapshot querySnapshot = await myRef.get();
  dataList.addAll(querySnapshot.docs.map((d) => MyUser.fromJson(d.data() as Map<String, dynamic>)));
  return dataList;
}

`

I tried to get data by Future<List> getData() {...} but List _filteredList = getData(); doesn't work as well as a lot of other tries. I only need to see a simple solution of my example working with firebase (StreamController a.s.o.) please. The best solution will include instead of searching in name searching in a String of 'firstName', 'lastName', and 'nickName'...

Tobicic
  • 1
  • 1
  • I don't see where you make a call to your firestore. If you're asking how to do that, I suggest taking some tutorials. That's too general a question to answer here. – jbryanh Nov 15 '22 at 14:59
  • Hi jbryanh, I changed the code and included firestore. – Tobicic Nov 15 '22 at 15:57

0 Answers0