0
votes

Comment afficher les données dans le fournisseur de FireStore à flutter

Je veux afficher des données de FireStore à l'aide du fournisseur de flutter. J'ai été coincé s'il vous plaît aide. Voici mes codes

// Page d'affichage du produit P>

    import 'package:cloud_firestore/cloud_firestore.dart';
    import 'package:shopping/models/product.dart';

    class ProductService{
        Firestore _firestore=Firestore.instance;
      String collection="Products";

      Future<List<Product>>getFeaturedProducts(){

     _firestore.collection(collection).where('featured', isEqualTo:true).getDocuments()
     .then((snap){

      List<Product>featuredProducts=[];
       snap.documents.map((snapshot)=> featuredProducts.add(Product.fromSnapshot(snapshot)));
       return featuredProducts;
     }); 

    }

    }


0 commentaires

3 Réponses :


0
votes

Vous n'appelez jamais _getfeaturéProduits () à partir de votre AppProvider classe. Donc _FEARTUREDPRODUCTS sera toujours null


2 commentaires

Comment appelle-je _getFeaturedProduits () s'il vous plaît aider


C'est votre propre code, vous devez obtenir les résultats de Firebase afin de construire la grille



0
votes

En classe AppProvider, vous appelez la méthode sur Nom non défini:

_productService.getFeaturedProducts()


0 commentaires

1
votes

Les gars, j'ai réussi à résoudre la question. La réponse est comme suit // Page
import 'package:flutter/material.dart';
import 'package:carousel_pro/carousel_pro.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:shopping/commons/common.dart';
import 'package:provider/provider.dart';
import 'package:shopping/provider/app_provider.dart';
import '../provider/user_provider.dart';

//My packages imports
import 'package:shopping/componets/horizontal_listview.dart';
import 'package:shopping/componets/product.dart';
import 'package:shopping/pages/cart.dart';
import 'package:shopping/pages/login.dart';

class HomePage extends StatefulWidget {
   // List<Product> products;

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

class _HomePageState extends State<HomePage> {
  TextEditingController _searchController = new TextEditingController();

  //final FirebaseAuth _firebaseAuth=FirebaseAuth.instance;

  @override
  Widget build(BuildContext context) {
   final user = Provider.of<UserProvider>(context);
   final productProvider=Provider.of<CRUDModel>(context);
    Widget image_carousel = new Container(
      height: 200.0,
      child: new Carousel(
        boxFit: BoxFit.cover,
        images: [
          AssetImage('images/c1.jpg'),
          AssetImage('images/m1.jpeg'),
          AssetImage('images/m2.jpg'),
          AssetImage('images/w1.jpeg'),
          AssetImage('images/w3.jpeg'),
          AssetImage('images/w4.jpeg'),
        ],
        autoplay:true,
        animationCurve: Curves.fastOutSlowIn,
        animationDuration: Duration(milliseconds:1000 ),
        dotSize: 4.0,
        indicatorBgPadding: 8.0,
        dotBgColor: Colors.transparent,
      ),
    );
    return Scaffold(
      appBar: new AppBar(
        iconTheme: IconThemeData(color: blue),
        elevation: 0.1,
        backgroundColor: white,
        title: Material(
          borderRadius: BorderRadius.circular(20),
          color: Colors.grey[50],
          elevation: 0.0,
          child: TextFormField(
              controller: _searchController,
              decoration: InputDecoration(
                hintText: "Search",
                border: InputBorder.none,
              ),
              validator: (value) {
                if (value.isEmpty) {
                  return "The Search field  cannot be empty";
                }
                return null;
              }),
        ),
        actions: <Widget>[
          new IconButton(
            icon: Icon(
              Icons.search,
              color: blue,
            ),
            onPressed: () {},
          ),
          new IconButton(
              icon: Icon(
                Icons.shopping_cart,
                color: blue,
              ),
              onPressed: () {
                Navigator.push(context,
                    MaterialPageRoute(builder: (context) => new Cart()));
              }),
        ],
      ),
      drawer: new Drawer(
        child: new ListView(
          children: <Widget>[
            //drawer header
            new UserAccountsDrawerHeader(
              accountName: Text("Afolabi"),
              accountEmail: Text("mtreal62@gmail.com"),
              currentAccountPicture: GestureDetector(
                child: new CircleAvatar(
                  backgroundColor: Colors.grey,
                  child: Icon(
                    Icons.person,
                    color: Colors.white,
                  ),
                ),
              ),
              decoration: BoxDecoration(
                color: blue,
              ),
            ),
            //body
            InkWell(
              onTap: () {},
              child: ListTile(
                title: Text("Home Page"),
                leading: Icon(
                  Icons.home,
                  color: blue,
                ),
              ),
            ),
            InkWell(
              onTap: () {},
              child: ListTile(
                title: Text("My Account"),
                leading: Icon(
                  Icons.person,
                  color: blue,
                ),
              ),
            ),
            InkWell(
              onTap: () {},
              child: ListTile(
                title: Text("My Orders"),
                leading: Icon(
                  Icons.shopping_basket,
                  color: blue,
                ),
              ),
            ),
            InkWell(
              onTap: () {
                Navigator.push(context,
                    MaterialPageRoute(builder: (context) => new Cart()));
              },
              child: ListTile(
                title: Text("Shopping Cart"),
                leading: Icon(
                  Icons.shopping_cart,
                  color: blue,
                ),
              ),
            ),
            InkWell(
              onTap: () {},
              child: ListTile(
                title: Text("Favourites"),
                leading: Icon(
                  Icons.favorite,
                  color: blue,
                ),
              ),
            ),
            Divider(),
            InkWell(
              onTap: () {},
              child: ListTile(
                title: Text("Settings"),
                leading: Icon(
                  Icons.settings,
                ),
              ),
            ),
            InkWell(
              onTap: () {},
              child: ListTile(
                title: Text("About"),
                leading: Icon(
                  Icons.help,
                ),
              ),
            ),

            InkWell(
              onTap: () {
               user.signOut();
        // changeScreenReplacement(context, Login());
              },
              child: ListTile(
                title: Text("Log Out"),
                leading: Icon(
                  Icons.transit_enterexit,
                ),
              ),
            ),
          ],
        ),
      ),
      body: new Column(
        children: <Widget>[
          //Image Carousel for the home Page Banner
          image_carousel,
          //padding widget after carousel
          new Padding(
            padding: const EdgeInsets.all(8.0),
            child: Container(
              alignment: Alignment.centerLeft,
              child: new Text("Categories"),
            ),
          ),
          //Horizontal layout start from here
          HorizontalList(),
          //End of the horizontal layout

          //padding widget for Recent products categories
          new Padding(
            padding: const EdgeInsets.all(8.0),
            child: Container(
              alignment: Alignment.centerLeft,
              child: new Text("Recent Products"),
            ),
          ),
              // Text(appProvider.featuredProducts.length.toString(),
                 //style: TextStyle(color: Colors.black),),

          Flexible(
            child: Products(),
          ),
          //Horizontal layout start from here
        ],
      ),
    );
  }
}

Future _signOut() async {
  try {
    await FirebaseAuth.instance.signOut();
  } catch (e) {
    print(e); // TODO: show dialog with error
  }
}


0 commentaires