Flutter 发生异常。NoSuchMethodError(NoSuchMethodError:getter';documents';在null上被调用。接收方:null尝试调用:documents)
我有两页。当我导航到另一个页面时,它有两个小部件,即类别和产品Flutter 发生异常。NoSuchMethodError(NoSuchMethodError:getter';documents';在null上被调用。接收方:null尝试调用:documents),flutter,google-cloud-firestore,flutter-dependencies,flutter-provider,flutter-change-notifier,Flutter,Google Cloud Firestore,Flutter Dependencies,Flutter Provider,Flutter Change Notifier,我有两页。当我导航到另一个页面时,它有两个小部件,即类别和产品 class HomeScreen extends StatelessWidget { static String routeName = "/home"; final DocumentSnapshot documentID; const HomeScreen({Key key, @required this.documentID}) : super(key: key); @override
class HomeScreen extends StatelessWidget {
static String routeName = "/home";
final DocumentSnapshot documentID;
const HomeScreen({Key key, @required this.documentID}) : super(key: key);
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
Provider<DocumentSnapshot>(create: (context) {
return documentID;
}),
ChangeNotifierProvider<NewId>(create: (context) {
return NewId();
})
],
child: new Scaffold(
backgroundColor: Colors.white,
body: Body(),
),
);
}
}
class Body extends StatelessWidget {
// final DocumentSnapshot documentSnapshot;
// const Body({Key key, @required this.documentSnapshot}) : super(key: key);
@override
Widget build(BuildContext context) {
return SafeArea(
child: SingleChildScrollView(
child: Column(
children: [
SizedBox(height: getProportionateScreenHeight(20)),
HomeHeader(),
SizedBox(height: getProportionateScreenWidth(5)),
DiscountBanner(),
Categories(
// documentSnapshot: documentSnapshot,
),
//SpecialOffers(),
SizedBox(height: getProportionateScreenWidth(15)),
PopularProducts(),
SizedBox(height: getProportionateScreenWidth(30)),
],
),
),
);
}
}
问题是当我从第一页传递navigate时,它显示为get文档被调用为null。到目前为止,我需要在索引[0]中显示产品的初始文档。但我不能将该文档作为一个价值来提及,因为我的产品和类别因公司而异。数据是通过firestore生成的。因为我的结构就像公司里有产品类别一样,在产品类别里有产品
class Categories extends StatefulWidget {
// final DocumentSnapshot documentSnapshot;
// const Categories({Key key, @required this.documentSnapshot})
// : super(key: key);
@override
_CategoriesState createState() => _CategoriesState();
}
class _CategoriesState extends State<Categories> {
@override
void initState() {
// TODO: implement initState
super.initState();
}
@override
void dispose() {
// TODO: implement dispose
super.dispose();
}
@override
Widget build(BuildContext context) {
return StreamBuilder(
stream: FirebaseFirestore.instance
.collection("Company-Master")
.doc(Provider.of<DocumentSnapshot>(context).id)
.collection("Products")
.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Center(
child: CircularProgressIndicator.adaptive(),
);
}
return Padding(
padding: EdgeInsets.symmetric(horizontal: 10),
child: Container(
height: 90,
width: double.infinity,
child: ListView.builder(
shrinkWrap: true,
physics: AlwaysScrollableScrollPhysics(),
scrollDirection: Axis.horizontal,
itemCount: snapshot.data.documents.length,
itemBuilder: (context, index) {
return Padding(
padding: EdgeInsets.symmetric(horizontal: 8),
child: CategoryCard(
icon: snapshot.data.documents[index]["PCategoryImage"],
text: snapshot.data.documents[index]["PCategoryName"],
press: () {
setState(() {
Provider.of<NewId>(context, listen: false)
.changeDocumentID(snapshot.data.documents[index]);
});
},
),
);
},
),
),
);
});
}
}
class CategoryCard extends StatelessWidget {
const CategoryCard({
Key key,
@required this.icon,
@required this.text,
this.press,
this.description,
}) : super(key: key);
final String icon, text, description;
final GestureTapCallback press;
@override
Widget build(BuildContext context) {
return InkWell(
onTap: press,
hoverColor: kTextColor,
child: SizedBox(
width: getProportionateScreenWidth(55),
child: Column(
children: [
Container(
padding: EdgeInsets.all(getProportionateScreenWidth(15)),
height: getProportionateScreenWidth(55),
width: getProportionateScreenWidth(55),
decoration: BoxDecoration(
color: Color(0xFFFFECDF),
borderRadius: BorderRadius.circular(10),
),
child: SvgPicture.network(icon),
),
SizedBox(height: 5),
Text(
text,
textAlign: TextAlign.center,
style: TextStyle(fontSize: getProportionateScreenWidth(10)),
)
],
),
),
);
}
}
class PopularProducts extends StatelessWidget {
final String getID;
const PopularProducts({Key key, this.getID}) : super(key: key);
@override
Widget build(BuildContext context) {
return StreamBuilder(
stream: FirebaseFirestore.instance
.collection("Company-Master")
.doc(Provider.of<DocumentSnapshot>(context).id)
.collection("Products")
.doc(Provider.of<NewId>(context).id)
.collection("Products")
.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
Center(
child: CircularProgressIndicator(),
);
}
if (snapshot.data == null) {
print("Fuck ya");
}
return Container(
height: 600,
child: ListView.builder(
itemCount: snapshot.data.documents.length,
itemBuilder: (context, index) {
if (snapshot.data.documents[index] == null) {
Center(
child: Text("Choose a category to view products"),
);
}
return ProductListColumn(
productName: snapshot.data.documents[index]["ProductName"],
);
},
),
);
});
}
}
class ProductListColumn extends StatelessWidget {
const ProductListColumn({
Key key,
@required this.productName,
}) : super(key: key);
final String productName;
@override
Widget build(BuildContext context) {
return InkWell(
onTap: () {
Navigator.of(context).push(
new MaterialPageRoute(
builder: (BuildContext context) => new ProductDetailScreen(
productName: productName,
),
// snapshot.data.documents[index]['SubName'],
// snapshot.data.documents[index].documentID),
),
);
},
child: Stack(
children: [
Container(
margin: EdgeInsets.all(10),
decoration: BoxDecoration(
color: Color(0xFFDECB9C),
borderRadius: BorderRadius.circular(20),
),
height: 130,
width: double.infinity,
child: Container(
height: 130,
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
color: Colors.white,
),
],
borderRadius: BorderRadius.circular(20),
),
margin: EdgeInsets.only(right: 10, bottom: 5),
),
),
Positioned(
right: 30,
bottom: 40,
child: Container(
height: 110,
width: 110,
decoration: BoxDecoration(
color: Color(0xFFDECB9C),
borderRadius: BorderRadius.circular(20),
),
child: Image.asset(
"assets/images/Image Popular Product 3.png",
fit: BoxFit.cover,
),
),
),
Positioned(
left: 30,
top: 60,
child: Container(
child: Text(
productName,
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w600,
color: Colors.black),
),
),
)
],
),
);
}
}
class NewId extends ChangeNotifier {
String id;
void changeDocumentID(DocumentSnapshot newID) {
id = newID.id;
notifyListeners();
}