【发布时间】:2021-07-18 18:45:56
【问题描述】:
Error: The method 'data' isn't defined for the class 'Object?'.
- 'Object' is from 'dart:core'.
Try correcting the name to the name of an existing method, or defining a method named 'data'.
Map _productMap = productSnap.data.data();
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:your_store/screens/product_page.dart';
import 'package:your_store/services/firebase_services.dart';
import 'package:your_store/widgets/custom_action_bar.dart';
class SavedTab extends StatelessWidget {
final FirebaseServices _firebaseServices = FirebaseServices();
@override
Widget build(BuildContext context) {
return Container(
child: Stack(
children: [
FutureBuilder<QuerySnapshot>(
future: _firebaseServices.usersRef
.doc(_firebaseServices.getUserId())
.collection("Saved")
.get(),
builder: (context, snapshot) {
if (snapshot.hasError) {
return Scaffold(
body: Center(
child: Text("Error: ${snapshot.error}"),
),
);
}
// Collection Data ready to display
if (snapshot.connectionState == ConnectionState.done) {
// Display the data inside a list view
return ListView(
padding: EdgeInsets.only(
top: 108.0,
bottom: 12.0,
),
children: snapshot.data!.docs.map((document) {
return GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ProductPage(
productId: document.id,
),
));
},
child: FutureBuilder(
future:
_firebaseServices.productRef.doc(document.id).get(),
builder: (context, productSnap) {
if (productSnap.hasError) {
return Container(
child: Center(
child: Text("${productSnap.error}"),
),
);
}
if (productSnap.connectionState ==
ConnectionState.done) {
Map _productMap = productSnap.data.data();
return Padding(
padding: const EdgeInsets.symmetric(
vertical: 16.0,
horizontal: 24.0,
),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Container(
width: 90,
height: 90,
child: ClipRRect(
borderRadius: BorderRadius.circular(8.0),
child: Image.network(
"${_productMap['images'][0]}",
fit: BoxFit.cover,
),
),
),
Container(
padding: EdgeInsets.only(
left: 16.0,
),
child: Column(
mainAxisAlignment:
MainAxisAlignment.start,
crossAxisAlignment:
CrossAxisAlignment.start,
children: [
Text(
"${_productMap['name']}",
style: TextStyle(
fontSize: 18.0,
color: Colors.black,
fontWeight: FontWeight.w600),
),
Padding(
padding: const EdgeInsets.symmetric(
vertical: 4.0,
),
child: Text(
"\$${_productMap['price']}",
style: TextStyle(
fontSize: 16.0,
color: Theme.of(context)
.accentColor,
fontWeight: FontWeight.w600),
),
),
Text(
"Size - ${document['size']}",
style: TextStyle(
fontSize: 16.0,
color: Colors.black,
fontWeight: FontWeight.w600),
),
],
),
),
],
),
);
}
return Container(
child: Center(
child: CircularProgressIndicator(),
),
);
},
),
);
}).toList(),
);
}
// Loading State
return Scaffold(
body: Center(
child: CircularProgressIndicator(),
),
);
},
),
CustomActionBar(
title: "Saved",
hasBackArrow: false,
),
],
),
);
}
}
【问题讨论】:
-
_firebaseServices.productRef是什么类型?您能否说明FirebaseServices类中如何定义该字段? -
这里的兄弟你可以从这里下载源代码:github.com/Bhaskar2510/Final-Sem还有一些错误,如果你能帮忙
标签: android flutter dart flutter-layout flutter-dependencies