简体   繁体   中英

Cant Retrieve more than one collection in Flutter from cloud firestore

Hy Guys what I am trying to do is to retrieve 2 collection documents from Firestore database and display into my flutter my code is:

Future<QuerySnapshot> getData() async {
    var firebaseUser = await FirebaseAuth.instance.currentUser();
    return await Firestore.instance
        .collection("user")
        .where("email", isEqualTo: firebaseUser.email)
        .getDocuments();
  }

  @override
  Widget build(BuildContext context) {
    // Build a Form widget using the _formKey created above.
    return FutureBuilder(
      future: getData(),

that allow me to retrieve data from a specific user but I have another form called "shippingAddress" which create collection as below:

void createRecord() async {
    await databaseReference.collection("shippingAddress").add({
      'alias': '$alias',
      'shippingName': '$shippingName',
      'shippinglastName': '$shippinglastName',
      'street': '$street',
      'streetNumber': '$streetNumber',
      'zipNumber': '$zipNumber',
      'phoneNumber': '$phoneNumber',
      'textMessage': '$textMessage',
      'totalQuantity': '$totalQuantity',
      'totalWeight': '$totalWeight',
    }).then((value) {
      print(value.documentID);
    });
  }

then I have to retrieve also the above collection but I dont know how... because in the same screen I retrieve "user" collection to display only data from a specific user and I need also to retrieve "shippingAddress" collection because in such collection there are other data to be displayed for example If I retrieve

${snapshot.data.documents[index].data["email"]} or ${snapshot.data.documents[index].data["name"]}

that I have got into signup form it works and into my widget it show the email or name but if I want for example retrieve:

${snapshot.data.documents[index].data["alias"]}

that is create into "shippingAddress" collection I cant get them

I have got myself the solution. First of all getData from existing user:

final databaseReference = Firestore.instance;

  Future<QuerySnapshot> getData() async {
    var firebaseUser = await FirebaseAuth.instance.currentUser();
    return await Firestore.instance
        .collection("user")
        .where("email", isEqualTo: firebaseUser.email)
        .getDocuments();
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: getData(),
      builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
        if (snapshot.connectionState == ConnectionState.done) {
          return ListView.builder(
            itemCount: snapshot.data.documents.length,
              itemBuilder: (BuildContext context, int index) {
                return Column(
                    children: <Widget>[
                    Stream(),

and call a Stream Builder with other collection:

lass Stream extends StatefulWidget {
  @override
  _StreamState createState() => _StreamState();
}

class _StreamState extends State<Stream> {
  @override
  Widget build(BuildContext context) {
    return StreamBuilder(
      stream: Firestore.instance.collection('shippingAddress').snapshots(),
      builder: (context, snapshot) {
        if (!snapshot.hasData)
          return Text('Loading data please Wait');
        return Column(
          children: <Widget>[
            Container(
              height: 1000,
              child: ListView.builder(
                itemCount: snapshot.data.documents.length,

thats all!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM