我是 Flutter 和 Firestore 的新手,我真的需要您的帮助来解决错误并构建我的项目! 当我按下汉堡包按钮和购物车按钮时出现相同的错误!
请帮帮我!
错误是
Performing hot restart
Syncing files to device Android SDK built for x86...
Restarted application in 1 543ms.
W/DynamiteModule(28079): Local module descriptor class for providerinstaller not found.
I/DynamiteModule(28079): Considering local module providerinstaller:0 and remote module
providerinstaller:0
W/ProviderInstaller(28079): Failed to load providerinstaller module: No acceptable module found.
Local version is 0 and remote version is 0.
======== Exception caught by widgets library =======================================================
The following NoSuchMethodError was thrown building Consumer<CartItemCounter>(dirty, dependencies:
[_InheritedProviderScope<CartItemCounter>]):
The getter 'length' was called on null.
Receiver: null
Tried calling: length
The relevant error-causing widget was:
Consumer<CartItemCounter> file:///C:/Users/Admin/Desktop/e_shop/lib/Store/storehome.dart:69:32
When the exception was thrown, this was the stack:
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5)
#1 new CartItemCounter (package:e_shop/Counters/cartitemcounter.dart:6:90)
#2 MyApp.build.<anonymous closure> (package:e_shop/main.dart:34:47)
#3 _CreateInheritedProviderState.value (package:provider/src/inherited_provider.dart:646:35)
#4 _InheritedProviderScopeElement.value (package:provider/src/inherited_provider.dart:509:33)
====================================================================================================
======== Exception caught by widgets library =======================================================
'package:flutter/src/painting/_network_image_io.dart': Failed assertion: line 25 pos 14: 'url !=
null': is not true.
====================================================================================================
======== Exception caught by widgets library =======================================================
The following assertion was thrown building MyDrawer(dirty):
'package:flutter/src/painting/_network_image_io.dart': Failed assertion: line 25 pos 14: 'url !=
null': is not true.
The relevant error-causing widget was:
MyDrawer file:///C:/Users/Admin/Desktop/e_shop/lib/Store/storehome.dart:86:17
When the exception was thrown, this was the stack:
#2 new NetworkImage (package:flutter/src/painting/_network_image_io.dart:25:14)
#3 MyDrawer.build (package:e_shop/Widgets/myDrawer.dart:37:40)
#4 StatelessElement.build (package:flutter/src/widgets/framework.dart:4758:28)
#5 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4684:15)
#6 Element.rebuild (package:flutter/src/widgets/framework.dart:4378:5)
====================================================================================================
======== Exception caught by widgets library =======================================================
The following NoSuchMethodError was thrown building Consumer2<TotalAmount, CartItemCounter>(dirty,
dependencies: [_InheritedProviderScope<TotalAmount>, _InheritedProviderScope<CartItemCounter>]):
The getter 'count' was called on null.
Receiver: null
Tried calling: count
The relevant error-causing widget was:
Consumer2<TotalAmount, CartItemCounter>
file:///C:/Users/Admin/Desktop/e_shop/lib/Store/cart.dart:60:20
When the exception was thrown, this was the stack:
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5)
#1 _CartPageState.build.<anonymous closure> (package:e_shop/Store/cart.dart:65:39)
#2 Consumer2.buildWithChild (package:provider/src/consumer.dart:201:19)
#3 SingleChildStatelessWidget.build (package:nested/nested.dart:260:41)
#4 StatelessElement.build (package:flutter/src/widgets/framework.dart:4758:28)
====================================================================================================
======== Exception caught by widgets library =======================================================
The getter 'length' was called on null.
Receiver: null
Tried calling: length
The relevant error-causing widget was:
Consumer<CartItemCounter> file:///C:/Users/Admin/Desktop/e_shop/lib/Widgets/customAppBar.dart:60:30
====================================================================================================
======== Exception caught by widgets library =======================================================
The following NoSuchMethodError was thrown building Consumer2<TotalAmount, CartItemCounter>(dirty,
dependencies: [_InheritedProviderScope<TotalAmount>, _InheritedProviderScope<CartItemCounter>]):
The getter 'count' was called on null.
Receiver: null
Tried calling: count
The relevant error-causing widget was:
Consumer2<TotalAmount, CartItemCounter>
file:///C:/Users/Admin/Desktop/e_shop/lib/Store/cart.dart:60:20
When the exception was thrown, this was the stack:
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5)
#1 _CartPageState.build.<anonymous closure> (package:e_shop/Store/cart.dart:65:39)
#2 Consumer2.buildWithChild (package:provider/src/consumer.dart:201:19)
#3 SingleChildStatelessWidget.build (package:nested/nested.dart:260:41)
#4 StatelessElement.build (package:flutter/src/widgets/framework.dart:4758:28)
===================================================================================================
======== Exception caught by widgets library =======================================================
'package:flutter/src/painting/_network_image_io.dart': Failed assertion: line 25 pos 14: 'url !=
null': is not true.
====================================================================================================
======== Exception caught by widgets library =======================================================
The following NoSuchMethodError was thrown building Consumer2<TotalAmount, CartItemCounter>(dirty,
dependencies: [_InheritedProviderScope<TotalAmount>, _InheritedProviderScope<CartItemCounter>]):
The getter 'count' was called on null.
Receiver: null
Tried calling: count
The relevant error-causing widget was:
Consumer2<TotalAmount, CartItemCounter>
file:///C:/Users/Admin/Desktop/e_shop/lib/Store/cart.dart:60:20
When the exception was thrown, this was the stack:
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5)
#1 _CartPageState.build.<anonymous closure> (package:e_shop/Store/cart.dart:65:39)
#2 Consumer2.buildWithChild (package:provider/src/consumer.dart:201:19)
#3 SingleChildStatelessWidget.build (package:nested/nested.dart:260:41)
#4 StatelessElement.build (package:flutter/src/widgets/framework.dart:4758:28)
====================================================================================================
I/BiChannelGoogleApi(28079): [FirebaseAuth: ] getGoogleApiForMethod() returned Gms:
com.google.firebase.auth.api.internal.zzao@490da8f
W/DynamiteModule(28079): Local module descriptor class for com.google.firebase.auth not found.
I/FirebaseAuth(28079): [FirebaseAuth:] Preparing to create service connection to gms implementation
D/FirebaseAuth(28079): Notifying id token listeners about user ( Cc77MTfLEKRs8L9exzWZND8i0Rg1 ).
storehome.dart
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:e_shop/Store/cart.dart';
import 'package:e_shop/Store/product_page.dart';
import 'package:e_shop/Counters/cartitemcounter.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:provider/provider.dart';
import 'package:e_shop/Config/config.dart';
import '../Widgets/loadingWidget.dart';
import '../Widgets/myDrawer.dart';
import '../Widgets/searchBox.dart';
import '../Models/item.dart';
double width;
class StoreHome extends StatefulWidget {
@override
_StoreHomeState createState() => _StoreHomeState();
}
class _StoreHomeState extends State<StoreHome> {
@override
Widget build(BuildContext context) {
width = MediaQuery.of(context).size.width;
return SafeArea(
child: Scaffold(
appBar: AppBar(
flexibleSpace: Container(
decoration: new BoxDecoration(
gradient: new LinearGradient(
colors: [Colors.blue, Colors.purpleAccent],
begin: const FractionalOffset (0.0, 0.0),
end: const FractionalOffset (1.0, 0.0),
stops: [0.0, 1.0],
tileMode: TileMode.clamp,
),
),
),
title: Text(
"Salam Market",
style: TextStyle(fontSize: 50.0, color: Colors.white, fontFamily: "Signatra"),
),
centerTitle: true,
actions: [
Stack(
children: [
IconButton(
icon: Icon(Icons.shopping_cart, color: Colors.white,),
onPressed: ()
{
Route route = MaterialPageRoute(builder: (c) => CartPage());
Navigator.pushReplacement(context, route);
},
),
Positioned(
child: Stack(
children: [
Icon(
Icons.brightness_1,
size: 20.0,
color: Colors.green,
),
Positioned(
top: 3.0,
bottom: 4.0,
left: 4.0,
child: Consumer<CartItemCounter>(
builder: (context, counter, _)
{
return Text(
(EcommerceApp.sharedPreferences.getStringList(EcommerceApp.userCartList).length-1).toString(),
style: TextStyle(color: Colors.white,fontSize: 12.0, fontWeight:
FontWeight.bold),
);
},
),
),
],
),
),
],
),
],
),
drawer: MyDrawer(),
body: CustomScrollView(
slivers: [
SliverPersistentHeader(pinned: true,delegate: SearchBoxDelegate()),
StreamBuilder<QuerySnapshot>(
stream:
FirebaseFirestore.instance.collection("items").limit(15).orderBy("publishedDate", descending:
true).snapshots(),
builder: (context, dataSnapshot)
{
return !dataSnapshot.hasData
? SliverToBoxAdapter(child: Center(child: circularProgress(),),)
: SliverStaggeredGrid.countBuilder(
crossAxisCount: 1,
staggeredTileBuilder: (c) => StaggeredTile.fit(1),
itemBuilder: (context, index)
{
ItemModel model = ItemModel.fromJson(dataSnapshot.data.docs[index].data());
return sourceInfo(model, context);
},
itemCount: dataSnapshot.data.docs.length,
);
},
),
],
),
),
);
}
}
Widget sourceInfo(ItemModel model, BuildContext context,
{Color background, removeCartFunction}) {
return InkWell(
onTap: ()
{
Route route = MaterialPageRoute(builder: (c) => ProductPage(itemModel: model));
Navigator.pushReplacement(context, route);
},
splashColor: Colors.pink,
child: Padding(
padding: EdgeInsets.all(6.0),
child: Container(
height: 190.0,
width: width,
child: Row(
children: [
Image.network(model.thumbnailUrl, width: 140.0, height: 140.0,),
SizedBox(width: 4.0,),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(),
Container(
child: Row(
mainAxisSize: MainAxisSize.max,
children: [
Expanded(
child: Text(model.title, style: TextStyle(color: Colors.black, fontSize:
14.0),),
),
],
),
),
SizedBox(height: 5.0,),
Container(
child: Row(
mainAxisSize: MainAxisSize.max,
children: [
Expanded(
child: Text(model.shortInfo, style: TextStyle(color: Colors.black54,
fontSize: 12.0),),
),
],
),
),
SizedBox(height: 20.0,),
Row(
children: [
Container(
decoration: BoxDecoration(
shape: BoxShape.rectangle,
color: Colors.pink,
),
alignment: Alignment.topLeft,
width: 40.0,
height: 43.0,
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"50%", style: TextStyle(fontSize: 15.0, color: Colors.white,
fontWeight: FontWeight.normal),
),
Text(
"OFF", style: TextStyle(fontSize: 12.0, color: Colors.white,
fontWeight: FontWeight.normal),
),
],
),
),
),
SizedBox(width: 10.0,),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: EdgeInsets.only(top: 0.0),
child: Row(
children: [
Text(
r"Öňki bahasy: $ ",
style: TextStyle(
fontSize: 14.0,
color: Colors.grey,
decoration: TextDecoration.lineThrough,
),
),
Text(
(model.price + model.price).toString(),
style: TextStyle(
fontSize: 15.0,
color: Colors.grey,
decoration: TextDecoration.lineThrough,
),
),
],
),
),
Padding(
padding: EdgeInsets.only(top: 5.0),
child: Row(
children: [
Text(
r"Täze bahasy: ",
style: TextStyle(
fontSize: 14.0,
color: Colors.grey,
),
),
Text(
"man ",
style: TextStyle(color: Colors.red, fontSize: 16.0),
),
Text(
(model.price).toString(),
style: TextStyle(
fontSize: 15.0,
color: Colors.grey,
),
),
],
),
),
],
),
],
),
Flexible(
child: Container(),
),
//to implement the cart item add/remove feature
Align(
alignment: Alignment.centerRight,
child: removeCartFunction == null
? IconButton(
icon: Icon(Icons.add_shopping_cart, color: Colors.pinkAccent,),
onPressed: ()
{
checkItemInCart(model.shortInfo, context);
},
)
: IconButton(
icon: Icon(Icons.delete, color: Colors.pinkAccent,),
onPressed: ()
{
removeCartFunction();
Route route = MaterialPageRoute(builder: (c) => StoreHome());
Navigator.pushReplacement(context, route);
},
),
),
Divider(
height: 5.0,
color: Colors.pink,
),
],
),
),
],
),
),
),
);
}
Widget card({Color primaryColor = Colors.redAccent, String imgPath}) {
return Container(
height: 150.0,
width: width * .34,
margin: EdgeInsets.symmetric(horizontal: 10, vertical: 10),
decoration: BoxDecoration(
color: primaryColor,
borderRadius: BorderRadius.all(Radius.circular(20.0)),
boxShadow: <BoxShadow>[
BoxShadow(offset: Offset(0, 5), blurRadius: 10.0, color: Colors.grey[200]),
]
),
child: ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(20.0)),
child: Image.network(
imgPath,
height: 150.0,
width: width * .34,
fit: BoxFit.fill,
),
),
);
}
void checkItemInCart(String shortInfoAsID, BuildContext context)
{
EcommerceApp.sharedPreferences.getStringList(EcommerceApp.userCartList).contains(shortInfoAsID)
? Fluttertoast.showToast(msg: "Haryt arabada")
: addItemToCart(shortInfoAsID, context);
}
addItemToCart(String shortInfoAsID, BuildContext context)
{
List tempCartList = EcommerceApp.sharedPreferences.getStringList(EcommerceApp.userCartList);
tempCartList.add(shortInfoAsID);
EcommerceApp.firestore.collection(EcommerceApp.collectionUser)
.doc(EcommerceApp.sharedPreferences.getString(EcommerceApp.userUID))
.update({
EcommerceApp.userCartList: tempCartList,
}).then((v){
Fluttertoast.showToast(msg: "Haryt araba goşuldy");
EcommerceApp.sharedPreferences.setStringList(EcommerceApp.userCartList, tempCartList);
Provider.of<CartItemCounter>(context, listen: false).displayResult();
});
}
myDrawer.dart
import 'package:e_shop/Authentication/authenication.dart';
import 'package:e_shop/Config/config.dart';
import 'package:e_shop/Address/addAddress.dart';
import 'package:e_shop/Store/Search.dart';
import 'package:e_shop/Store/cart.dart';
import 'package:e_shop/Orders/myOrders.dart';
import 'package:e_shop/Store/storehome.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class MyDrawer extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Drawer(
child: ListView(
children: [
Container(
padding: EdgeInsets.only(top: 25.0, bottom: 10.0),
decoration: new BoxDecoration(
gradient: new LinearGradient(
colors: [Colors.blue, Colors.purpleAccent],
begin: const FractionalOffset (0.0, 0.0),
end: const FractionalOffset (1.0, 0.0),
stops: [0.0, 1.0],
tileMode: TileMode.clamp,
),
),
child: Column(
children: [
Material(
borderRadius: BorderRadius.all(Radius.circular(80.0)),
elevation: 8.0,
child: Container(
height: 160.0,
width: 160.0,
child: CircleAvatar(
backgroundImage: NetworkImage(
EcommerceApp.sharedPreferences.getString(EcommerceApp.userAvatarUrl),
),
),
),
),
SizedBox(height: 10.0,),
Text(
EcommerceApp.sharedPreferences.getString(EcommerceApp.userName),
style: TextStyle(color: Colors.white, fontSize: 35.0, fontFamily: "Signatra"),
),
],
),
),
SizedBox(height: 12.0,),
Container(
padding: EdgeInsets.only(top: 1.0),
decoration: new BoxDecoration(
gradient: new LinearGradient(
colors: [Colors.blue, Colors.purpleAccent],
begin: const FractionalOffset (0.0, 0.0),
end: const FractionalOffset (1.0, 0.0),
stops: [0.0, 1.0],
tileMode: TileMode.clamp,
),
),
child: Column(
children: [
ListTile(
leading: Icon(Icons.home, color: Colors.white,),
title: Text("Esasy", style: TextStyle(color: Colors.white),),
onTap: (){
Route route = MaterialPageRoute(builder: (c) => StoreHome());
Navigator.pushReplacement(context, route);
},
),
Divider(height: 10.0, color: Colors.white, thickness: 6.0,),
ListTile(
leading: Icon(Icons.reorder, color: Colors.white,),
title: Text("Zakazlarym", style: TextStyle(color: Colors.white),),
onTap: (){
Route route = MaterialPageRoute(builder: (c) => MyOrders());
Navigator.pushReplacement(context, route);
},
),
Divider(height: 10.0, color: Colors.white, thickness: 6.0,),
ListTile(
leading: Icon(Icons.shopping_cart, color: Colors.white,),
title: Text("Arabam", style: TextStyle(color: Colors.white),),
onTap: (){
Route route = MaterialPageRoute(builder: (c) => CartPage());
Navigator.pushReplacement(context, route);
},
),
Divider(height: 10.0, color: Colors.white, thickness: 6.0,),
ListTile(
leading: Icon(Icons.search, color: Colors.white,),
title: Text("Gözlemek", style: TextStyle(color: Colors.white),),
onTap: (){
Route route = MaterialPageRoute(builder: (c) => SearchProduct());
Navigator.pushReplacement(context, route);
},
),
Divider(height: 10.0, color: Colors.white, thickness: 6.0,),
ListTile(
leading: Icon(Icons.add_location, color: Colors.white,),
title: Text("Täze salgy goşmak", style: TextStyle(color: Colors.white),),
onTap: (){
Route route = MaterialPageRoute(builder: (c) => AddAddress());
Navigator.pushReplacement(context, route);
},
),
Divider(height: 10.0, color: Colors.white, thickness: 6.0,),
ListTile(
leading: Icon(Icons.exit_to_app, color: Colors.white,),
title: Text("Çykmak", style: TextStyle(color: Colors.white),),
onTap: (){
EcommerceApp.auth.signOut().then((c){
Route route = MaterialPageRoute(builder: (c) => AuthenticScreen());
Navigator.pushReplacement(context, route);
});
},
),
Divider(height: 10.0, color: Colors.white, thickness: 6.0,),
],
),
),
],
),
);
}
}
cartitemcounter.dart
import 'package:flutter/foundation.dart';
import 'package:e_shop/Config/config.dart';
class CartItemCounter extends ChangeNotifier
{
int _counter = EcommerceApp.sharedPreferences.getStringList(EcommerceApp.userCartList).length-1;
int get count => _counter;
Future<void> displayResult() async
{
_counter = EcommerceApp.sharedPreferences.getStringList(EcommerceApp.userCartList).length-1;
await Future.delayed(const Duration(milliseconds: 100), (){
notifyListeners();
});
}
}
如果这些代码不足以识别错误,我可以显示 main.dart 页面!
config.dart 页面
class EcommerceApp
{
static const String appName = 'e-Shop';
static SharedPreferences sharedPreferences;
static User user;
static FirebaseAuth auth;
static FirebaseFirestore firestore ;
static String collectionUser = "users";
static String collectionOrders = "orders";
static String userCartList = 'userCart';
static String subCollectionAddress = 'userAddress';
static final String userName = 'name';
static final String userEmail = 'email';
static final String userPhotoUrl = 'photoUrl';
static final String userUID = 'uid';
static final String userAvatarUrl = 'url';
static final String addressID = 'addressID';
static final String totalAmount = 'totalAmount';
static final String productID = 'productIDs';
static final String paymentDetails ='paymentDetails';
static final String orderTime ='orderTime';
static final String isSuccess ='isSuccess';
}
答案 0 :(得分:0)
今天一直在为同样的问题苦苦挣扎
这是一个解决方案:
代替
(EcommerceApp.sharedPreferences.getStringList(EcommerceApp.userCartList).length-1).toString(),
在您的 Storehome.dart 文件中,改用它
counter?.count?.toString(),