Closed nopemyrobin closed 3 years ago
Hi @nopemyrobin You can post some lines of code to reproduce this issue
This is button when I click:
RaisedButton.icon(
onPressed: () {
Provider.of<CartBloc>(context, listen: false)
.addCardManager(id, name, imgsmall, price, _n);
},
color: Colors.orangeAccent,
splashColor: transparentBlue,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)),
label: Text("Add to Cart"),
icon: Icon(Icons.add_shopping_cart))
Error comment:
When the exception was thrown, this was the stack
package:ogitu/…/shop/product_detail_page.dart:180
package:flutter/…/material/ink_well.dart:991
package:flutter/…/gestures/recognizer.dart:182
package:flutter/…/gestures/tap.dart:607 ... Handler: "onTap" Recognizer: TapGestureRecognizer#1176e debugOwner: GestureDetector state: possible won arena finalPosition: Offset(276.0, 764.0) finalLocalPosition: Offset(68.0, 26.0) button: 1 sent tap down
What is the part of Provider< CartBloc>.factory
or Provider< CartBloc>.value
:)) ?
I have cart_bloc.dart
`import 'dart:collection';
import 'package:flutter/material.dart';
class CartBloc with ChangeNotifier {
List<Cart> _addNewData = [];
int get newDataCount {
return _addNewData.length;
}
UnmodifiableListView<Cart> get tasks {
return UnmodifiableListView(_addNewData);
}
void addCardManager(String id, String itemName, String assetName,
double ogRate, int quantity) {
final newAddItem = Cart(
id: id,
name: itemName,
ogPrice: ogRate,
asset: assetName,
quantity: quantity);
if (_addNewData.length != 0) {
bool isFound = false;
for (int itemcount = 0; itemcount < newDataCount; itemcount++) {
if (_addNewData[itemcount].name == newAddItem.name) {
print("addCard");
isFound = true;
_addNewData[itemcount].toggleDone();
notifyListeners();
break;
}
}
if (!isFound) {
_addNewData.add(newAddItem);
notifyListeners();
}
} else {
_addNewData.add(newAddItem);
notifyListeners();
}
}
void removeCardManager(String id, String itemName, String assetName,
double ogRate, int quantity) {
final newAddItem = Cart(
id: id,
name: itemName,
ogPrice: ogRate,
asset: assetName,
quantity: quantity);
print(_addNewData);
if (_addNewData.length != 0) {
bool isFound = false;
for (int itemcount = 0; itemcount < newDataCount; itemcount++) {
if (_addNewData[itemcount].name == newAddItem.name) {
print("RemoveCard");
isFound = true;
decrease(_addNewData[itemcount]);
notifyListeners();
break;
}
}
}
}
void updateTask(Cart task) {
task.toggleDone();
notifyListeners();
}
void decrease(Cart task) {
if (task.quantity == 1) {
removeCard(task);
}
task.decreaseDown();
notifyListeners();
}
void removeCard(Cart task) {
_addNewData.remove(task);
notifyListeners();
}
}
class Cart {
final String id;
final String name;
final String asset;
final double ogPrice;
int quantity;
Cart({
this.id,
this.name,
this.asset,
this.ogPrice,
this.quantity = 1,
});
void toggleDone() {
quantity++;
}
void decreaseDown() {
// ignore: unnecessary_statements
quantity == 0 ? 0 : quantity--;
}
}
You can try this one
Provider<CartBloc>.factory(
(context) => CartBloc(),
child: MaterialApp(...)
);
Nice, thanks
Hello Master,
I found error on my code like this: Error: No Provider found. To fix, please try:
Any can help me?