rrousselGit / provider

InheritedWidgets, but simple
https://pub.dev/packages/provider
MIT License
5.1k stars 509 forks source link

Error #534

Closed shehriyarmalik closed 3 years ago

shehriyarmalik commented 4 years ago

So I am trying to retrieve data from Firebase however I am encountering error. I am getting this error:

[VERBOSE-2:ui_dart_state.cc(166)] Unhandled Exception: type 'int' is not a subtype of type 'double'

0 ProductProvider.getHomeAchiveData. (package:final_ecommerce/provider/product_provider.dart:139:34)

1 List.forEach (dart:core-patch/growable_array.dart:302:8)

2 ProductProvider.getHomeAchiveData (package:final_ecommerce/provider/product_provider.dart:134:26)

#3 _HomeScreentate.getCallAllFunction (package:final_ecommerce/new_home.dart:210:21) #4 _HomeScreentate.build (package:final_ecommerce/new_home.dart:217:5) #5 StatefulElement.build (package:flutter/src/widgets/framework.dart:4663:28) #6 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4546:15) #7 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4719:11) #8 Element.rebuild (package:flutter/src/widgets/framework.dart:4262:5) #9 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4<…> [VERBOSE-2:ui_dart_state.cc(166)] Unhandled Exception: type 'int' is not a subtype of type 'double' #0 ProductProvider.getHomeFeatureData. (package:final_ecommerce/provider/product_provider.dart:116:36) #1 List.forEach (dart:core-patch/growable_array.dart:302:8) #2 ProductProvider.getHomeFeatureData (package:final_ecommerce/provider/product_provider.dart:111:9) #3 _HomeScreentate.getCallAllFunction (package:final_ecommerce/new_home.dart:209:21) #4 _HomeScreentate.build (package:final_ecommerce/new_home.dart:217:5) #5 StatefulElement.build (package:flutter/src/widgets/framework.dart:4663:28) #6 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4546:15) #7 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4719:11) #8 Element.rebuild (package:flutter/src/widgets/framework.dart:4262:5) #9 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:<…> [VERBOSE-2:ui_dart_state.cc(166)] Unhandled Exception: type 'int' is not a subtype of type 'double' #0 ProductProvider.getNewAchiveData. (package:final_ecommerce/provider/product_provider.dart:165:34) #1 List.forEach (dart:core-patch/growable_array.dart:302:8) #2 ProductProvider.getNewAchiveData (package:final_ecommerce/provider/product_provider.dart:160:26) #3 _HomeScreentate.getCallAllFunction (package:final_ecommerce/new_home.dart:207:21) #4 _HomeScreentate.build (package:final_ecommerce/new_home.dart:217:5) #5 StatefulElement.build (package:flutter/src/widgets/framework.dart:4663:28) #6 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4546:15) #7 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4719:11) #8 Element.rebuild (package:flutter/src/widgets/framework.dart:4262:5) #9 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:452<…> [VERBOSE-2:profiler_metrics_ios.mm(184)] Error retrieving thread information: (ipc/send) invalid destination port This is my code: Home: ``` class HomeScreen extends StatefulWidget { @override _HomeScreentate createState() => _HomeScreentate(); } Product menData; CategoryProvider categoryProvider; ProductProvider productProvider; Product womenData; Product bulbData; Product smartPhoneData; class _HomeScreentate extends State { double height, width; bool homeColor = true; bool checkoutColor = false; bool aboutColor = false; bool contactUsColor = false; bool profileColor = false; MediaQueryData mediaQuery; Widget _buildImageSlider() { return Container( height: height * 0.3, child: Carousel( autoplay: true, showIndicator: false, images: [ AssetImage("images/man.jpg"), AssetImage("images/women.jpg"), AssetImage("images/camera.jpg"), ], ), ); } Widget _buildFeature() { List featureProduct; featureProduct = productProvider.getFeatureList; return Column( children: [ Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( "Featured", style: TextStyle(fontSize: 17, fontWeight: FontWeight.bold), ), GestureDetector( onTap: () { Navigator.of(context).pushReplacement( MaterialPageRoute( builder: (ctx) => ListProduct( name: "Featured", isCategory: false, snapShot: featureProduct, ), ), ); }, child: Text( "View more", style: TextStyle(fontSize: 17, fontWeight: FontWeight.bold), ), ) ], ), Row( children: productProvider.getHomeFeatureList.map((e) { return Expanded( child: Row( children: [ Expanded( child: GestureDetector( onTap: () {}, child: SingleProduct( image: e.image, price: e.price, name: e.name, ), ), ), GestureDetector( onTap: () {}, child: SingleProduct( image: e.image, price: e.price, name: e.name, ), ), ], ), ); }).toList(), ), ], ); } Widget _buildNewAchives() { List newAchivesProduct = productProvider.getNewAchiesList; return Column( children: [ Container( height: height * 0.1 - 30, child: Column( mainAxisAlignment: MainAxisAlignment.end, children: [ Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( "New Achives", style: TextStyle(fontSize: 17, fontWeight: FontWeight.bold), ), GestureDetector( onTap: () { Navigator.of(context).pushReplacement( MaterialPageRoute( builder: (ctx) => ListProduct( name: "NewAchvies", isCategory: false, snapShot: newAchivesProduct, ), ), ); }, child: Text( "View more", style: TextStyle(fontSize: 17, fontWeight: FontWeight.bold), ), ) ], ), ], ), ), Row( children: productProvider.getHomeAchiveList.map((e) { return Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( children: [ Expanded( child: Row( children: [ Expanded( child: GestureDetector( onTap: () {}, child: SingleProduct( image: e.image, price: e.price, name: e.name), ), ), GestureDetector( onTap: () {}, child: SingleProduct( image: e.image, price: e.price, name: e.name), ) ], ), ) ], ), ], ), ); }).toList()), ], ); } final GlobalKey _key = GlobalKey(); void getCallAllFunction() { productProvider.getNewAchiveData(); productProvider.getFeatureData(); productProvider.getHomeFeatureData(); productProvider.getHomeAchiveData(); } @override Widget build(BuildContext context) { categoryProvider = Provider.of(context); productProvider = Provider.of(context); getCallAllFunction(); height = MediaQuery.of(context).size.height; width = MediaQuery.of(context).size.width; return Scaffold( key: _key, appBar: AppBar( title: Text( "HomePage", style: TextStyle(color: Colors.black), ), centerTitle: true, elevation: 0.0, backgroundColor: Colors.grey[100], leading: IconButton( icon: Icon( Icons.menu, color: Colors.black, ), onPressed: () {}), actions: [ NotificationButton(), ], ), body: Container( height: double.infinity, width: double.infinity, margin: EdgeInsets.symmetric(horizontal: 20), child: ListView( children: [ Container( width: double.infinity, child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ _buildImageSlider(), SizedBox( height: 20, ), _buildFeature(), _buildNewAchives() ], ), ) ], ), ), ); } } ``` And Provider model looks like this ``` class ProductProvider with ChangeNotifier { List feature = []; Product featureData; List checkOutModelList = []; CartModel checkOutModel; List userModelList = []; UserModel userModel; Future getUserData() async { List newList = []; User currentUser = FirebaseAuth.instance.currentUser; QuerySnapshot userSnapShot = await FirebaseFirestore.instance.collection("User").get(); userSnapShot.docs.forEach( (element) { if (currentUser.uid == element.data()["UserId"]) { userModel = UserModel( userAddress: element.data()["UserAddress"], userImage: element.data()["UserImage"], userEmail: element.data()["UserEmail"], userGender: element.data()["UserGender"], userName: element.data()["UserName"], userPhoneNumber: element.data()["UserNumber"]); newList.add(userModel); } userModelList = newList; }, ); } List get getUserModelList { return userModelList; } void deleteCheckoutProduct(int index) { checkOutModelList.removeAt(index); notifyListeners(); } void clearCheckoutProduct() { checkOutModelList.clear(); notifyListeners(); } void getCheckOutData({ int quentity, double price, String name, String color, String size, String image, }) { checkOutModel = CartModel( color: color, size: size, price: price, name: name, image: image, quentity: quentity, ); checkOutModelList.add(checkOutModel); } List get getCheckOutModelList { return List.from(checkOutModelList); } int get getCheckOutModelListLength { return checkOutModelList.length; } Future getFeatureData() async { List newList = []; QuerySnapshot featureSnapShot = await FirebaseFirestore.instance .collection("products") .doc("hfPmMokn0tbAuGZvRMy1") .collection("featureproduct") .get(); featureSnapShot.docs.forEach( (element) { featureData = Product( image: element.data()["image"], name: element.data()["name"], price: element.data()["price"]); print(featureSnapShot); newList.add(featureData); }, ); feature = newList; } List get getFeatureList { return feature; } List homeFeature = []; Future getHomeFeatureData() async { List newList = []; QuerySnapshot featureSnapShot = await FirebaseFirestore.instance.collection("homefeature").get(); featureSnapShot.docs ..forEach( (element) { featureData = Product( image: element.data()["image"], name: element.data()["name"], price: element.data()["price"]); newList.add(featureData); }, ); homeFeature = newList; notifyListeners(); } List get getHomeFeatureList { return homeFeature; } List homeAchive = []; Future getHomeAchiveData() async { List newList = []; QuerySnapshot featureSnapShot = await FirebaseFirestore.instance.collection("homeachive").get(); featureSnapShot.docs.forEach( (element) { featureData = Product( image: element.data()["image"], name: element.data()["name"], price: element.data()["price"]); newList.add(featureData); }, ); homeAchive = newList; notifyListeners(); } List get getHomeAchiveList { return homeAchive; } List newAchives = []; Product newAchivesData; Future getNewAchiveData() async { List newList = []; QuerySnapshot achivesSnapShot = await FirebaseFirestore.instance .collection("products") .doc("hfPmMokn0tbAuGZvRMy1") .collection("newachives") .get(); achivesSnapShot.docs.forEach( (element) { newAchivesData = Product( image: element.data()["image"], name: element.data()["name"], price: element.data()["price"]); newList.add(newAchivesData); }, ); newAchives = newList; notifyListeners(); } List get getNewAchiesList { return newAchives; } List notificationList = []; void addNotification(String notification) { notificationList.add(notification); } int get getNotificationIndex { return notificationList.length; } get getNotificationList { return notificationList; } List searchList; void getSearchList({List list}) { searchList = list; } List searchProductList(String query) { List searchShirt = searchList.where((element) { return element.name.toUpperCase().contains(query) || element.name.toLowerCase().contains(query); }).toList(); return searchShirt; } } ```
vinodpahumalani commented 4 years ago

@shehriyarmalik try converting element.data()["price"] into a double in your file product_provider.dart, line:131

Future getHomeAchiveData() async { List newList = []; QuerySnapshot featureSnapShot = await FirebaseFirestore.instance.collection("homeachive").get(); featureSnapShot.docs.forEach( (element) { featureData = Product( image: element.data()["image"], name: element.data()["name"], price: element.data()["price"]); <------- this might be int convert to double newList.add(featureData); }, ); homeAchive = newList; notifyListeners(); }

vinodpahumalani commented 4 years ago

If the above answer doesn't help you, then also this error is not related to the provider.

thechinkysight commented 4 years ago

[VERBOSE-2:ui_dart_state.cc(166)] Unhandled Exception: type 'int' is not a subtype of type 'double' The second line of your issue itself is very clear, it says you are trying to pass or return a int value to a function or a variable which takes or is of type double. It is not related to Provider.

rrousselGit commented 3 years ago

Closing as this is unrelated to provider and instead a bug in your code (apparently the getHomeAchiveData method).

If you still have a problem, consider asking your question on StackOverflow