1

I already have data stored on firestore , but now i'd like to add this data into an empty list using ChangeNotifier , similar to this example -https://docs.flutter.dev/development/data-and-backend/state-mgmt/simple .

so below i'd like to add data stored on firebase into the _cart list , with the method I tried below I got the error The argument type 'List<Menu>' can't be assigned to the parameter type 'Menu'(would like to know why?) , it also contains the ui for where i'd like to map the data from cart list into the dialog sheet:

class AddList extends ConsumerWidget {
  const AddList({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final menuAsync = ref.watch(menuProvider);
    final model = ref.read(cartProvider);

    return Scaffold(
      appBar: AppBar(
        title: const Text("menu"),
      ),
      floatingActionButton: FloatingActionButton(
        backgroundColor: Colors.black,
        onPressed: () async {
          showDialog(
              context: context,
              builder: (context) {
                return AlertDialog(
                  title: const Text("cart"),
                  content: Column(
                    children: const [

...model.cart.map((item) => Text(item.mealName)),

                    ],
                  ),
                );
              });
        },
      ),
      body: menuAsync.when(
          data: (menu) => Column(
                children: menu
                    .map(
                      (e) => Card(
                          child: ListTile(
                              title: Text(e.mealName),
                              subtitle: Text(e.price),
                              trailing: IconButton(
                                  onPressed: () {
                                    model.addProduct(menu);//where im getting error
                                  },
                                  icon: const Icon(Icons.add)))),
                    )
                    .toList(),
              ),
          error: (e, s) => Center(child: Text("$e")),
          loading: () => const Center(child: CircularProgressIndicator())),
    );
  }
}

below im using changenotifier to modify the cart list:

final cartProvider =
    ChangeNotifierProvider<CartNotifier>((ref) => CartNotifier());

class CartNotifier extends ChangeNotifier {
  final List<Menu> _cart = [];

  List<Menu> get cart => _cart;

  void addProduct(Menu menu) {
    _cart.add(menu);
    notifyListeners();
  }

  void removeProduct(Menu menu) {
    _cart.remove(menu);
    notifyListeners();
  }
}

how im reading data from firestore :

final menuProvider = StreamProvider<List<Menu>>(
    (ref) => ref.read(addMealRespositoryProvider).menuSearchStream);

    Stream<List<Menu>> get menuSearchStream =>
          _firestore.collection("menu").snapshots().map(
                (event) => event.docs.map((e) => Menu.fromFirestore(e)).toList(),
              );

snippet of my data model:

class Menu {
  String mealName;
  String price;

 Menu({
    required this.mealName,
    required this.price,

});
Map<String, dynamic> toMap() {
    return {
      "mealName": mealName,
      "price": price, },

factory Menu.fromFirestore(DocumentSnapshot doc) {
    final map = doc.data() as Map<String, dynamic>;
    return Menu(
      mealName: map["mealName"] ?? '',
      price: map["price"] ?? '',

);
}

}
hayek
  • 33
  • 4

0 Answers0