hoc081098 / flutter_provider

:octocat: Flutter generic provider using InheritedWidget. Flutter generic provider using InheritedWidget. An helper to easily exposes a value using InheritedWidget without having to write one.
https://pub.dev/packages/flutter_provider
MIT License
5 stars 1 forks source link

Error Provider 2.0.0 #5

Closed nopemyrobin closed 3 years ago

nopemyrobin commented 3 years ago

Hello Master,

I found error on my code like this: Error: No Provider found. To fix, please try:

Any can help me?

hoc081098 commented 3 years ago

Hi @nopemyrobin You can post some lines of code to reproduce this issue

nopemyrobin commented 3 years ago

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

0 Provider.of (package:flutter_provider/src/flutter_provider.dart:88:7)

1 _ProductDetailPageState.build.

package:ogitu/…/shop/product_detail_page.dart:180

2 _InkResponseState._handleTap

package:flutter/…/material/ink_well.dart:991

3 GestureRecognizer.invokeCallback

package:flutter/…/gestures/recognizer.dart:182

4 TapGestureRecognizer.handleTapUp

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

hoc081098 commented 3 years ago

What is the part of Provider< CartBloc>.factory or Provider< CartBloc>.value :)) ?

nopemyrobin commented 3 years ago

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--;
  }
}
hoc081098 commented 3 years ago

You can try this one

Provider<CartBloc>.factory(
  (context) => CartBloc(),
  child: MaterialApp(...)
);
nopemyrobin commented 3 years ago

Nice, thanks