#android #flutter #dart #error-handling #custom-error-handling
Вопрос:
Привет, я пытаюсь извлечь данные из firebase и отобразить их в ListView, но у меня возникла ошибка, как ее решить? вот мой код, Ошибка появляется в streambuilder, я думаю, из того, что я опубликовал, похоже, что я пытаюсь захватить поле из коллекции. sub, я удостоверяюсь, что идентификатор документа правильный, а также имена полей для него, чтобы вытащить их.
class TimelineScreen extends StatefulWidget {
TimelineScreen({Key? key}) : super(key: key);
@override
_TimelineScreenState createState() => _TimelineScreenState();
}
class _TimelineScreenState extends State<TimelineScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
decoration: BoxDecoration(
color: Theme.of(context).primaryColor,
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(10),
bottomRight: Radius.circular(10),
),
),
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.only(left: 15.0, top: 20, right: 10),
child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
"# Top popularité",
style: TextStyle(
fontSize: 12,
color: Colors.grey,
),
),
IconButton(
icon: Icon(Icons.leaderboard_rounded,
color: Colors.grey),
onPressed: () {},
)
],
),
Container(
height: 85,
margin: EdgeInsets.only(
top: 8,
left: 16,
bottom: 8,
),
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: data_post.length,
itemBuilder: (context, index) {
return Container(
width: 65,
margin: EdgeInsets.only(left: 16),
child: Column(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: [
Stack(
children: [
Container(
height: 60,
width: 60,
padding: EdgeInsets.all(2),
decoration: BoxDecoration(
image: DecorationImage(
fit: BoxFit.cover,
image: AssetImage(
data_post[index]['profilPic'],
)),
borderRadius: BorderRadius.all(
Radius.circular(30),
),
color:
Theme.of(context).primaryColor,
border: Border.all(
width: 3,
color:
Colors.deepPurpleAccent)),
),
Positioned(
bottom: 0,
right: 0,
child: Container(
height: 18,
width: 18,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(
Radius.circular(9),
),
color: Colors.green,
border: Border.all(
color: Theme.of(context)
.primaryColor,
width: 2)),
),
)
],
),
Text(data_post[index]['username'],
style: TextStyle(
fontSize: 12,
fontFamily: 'Robotto',
fontWeight: FontWeight.w400,
color: Theme.of(context).accentColor))
],
),
);
}),
)
],
),
),
Divider(),
Padding(
padding: const EdgeInsets.all(10.0),
child: Text(
"# Actualité",
style: TextStyle(
fontSize: 12,
color: Colors.grey,
),
),
),
Padding(
padding: const EdgeInsets.all(10.0),
child: StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance
.collection(POST_COLLECTION)
.snapshots(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(
child: SpinKitDoubleBounce(
color: Colors.deepPurpleAccent, size: 50));
} else {
return loadPosts(context, snapshot);
}
},
),
)
],
),
),
),
);
}
Widget loadPosts(
BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
return Wrap(
spacing: 10,
runSpacing: 10,
children: [
ListView(
children: snapshot.data!.docs.map((documentSnapshot) {
return GestureDetector(
onTap: () {
Navigator.of(context).push(MaterialPageRoute(builder: (context) {
return TimelineInfo(
imagepost: documentSnapshot.get('photoUrl'));
}));
},
child: Container(
width: (MediaQuery.of(context).size.width - 30) / 2,
height: 250,
child: Stack(
children: [
Container(
width: (MediaQuery.of(context).size.width - 30) / 2,
height: 250,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
),
child: Hero(
tag: 'location-img-${"url"}',
child: Image.network(documentSnapshot.get('photoUrl'),
fit: BoxFit.cover))),
Container(
width: (MediaQuery.of(context).size.width - 15) / 2,
height: 250,
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [
Colors.black.withOpacity(0.3),
Colors.black.withOpacity(0)
],
end: Alignment.topCenter,
begin: Alignment.bottomCenter),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
ListTile(
title: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Icon(Icons.star, color: Colors.white, size: 12),
SizedBox(
width: 3,
),
Text("",
style: TextStyle(
fontFamily: 'Robotto',
color: Colors.white,
fontSize: 12))
],
),
subtitle: Padding(
padding: const EdgeInsets.only(left: 3.0),
child: Text("12",
style: TextStyle(
color: Colors.white.withOpacity(0.7),
fontSize: 10,
fontWeight: FontWeight.bold)),
),
trailing: GestureDetector(
onTap: () {
void showBottom() => showModalBottomSheet(
enableDrag: true,
isDismissible: true,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(30),
topRight: Radius.circular(20)),
),
context: context,
builder: (context) => Column(
mainAxisSize: MainAxisSize.min,
children: [
SizedBox(height: 10),
HandleWidget(),
ListTile(
leading: Stack(
children: [
CircleAvatar(
radius: 40,
backgroundColor:
Theme.of(context)
.accentColor,
backgroundImage: NetworkImage(
documentSnapshot
.get('photoUrl')),
),
Positioned(
bottom: 0,
right: 12,
child: CircleAvatar(
radius: 7,
backgroundColor:
Theme.of(context)
.primaryColor,
child: CircleAvatar(
radius: 5,
backgroundColor:
Colors.red,
),
))
],
),
title: Text(
"username",
style: TextStyle(
fontSize: 20,
fontFamily: 'Robotto'),
),
subtitle: Row(
children: [
Icon(Feather.map_pin, size: 12),
SizedBox(width: 5),
Text("region"),
],
),
trailing: IconButton(
onPressed: () {},
icon: Icon(Feather.user)),
),
Divider(),
UserStats(
likes: "30",
friends: "30",
popularite: "30"),
Divider(),
],
));
showBottom();
},
child: CircleAvatar(
radius: 20,
backgroundColor: Theme.of(context).accentColor,
backgroundImage: NetworkImage(
documentSnapshot.get('photoUrl')),
),
),
),
],
),
)
],
),
),
);
}).toList()),
],
);
}
}
и
String postId = "${userId}_${name}";
Map<String, dynamic> postData = {
"type": "video",
"description": description,
"uid": userId,
"time": Timestamp.now(),
"postUrl": uploadPostVideoUrl,
"postId": postId,
};
await FirebaseFirestore.instance
.collection(POST_COLLECTION)
.doc(postId)
.set(postData, SetOptions(merge: true));
вот в чем ошибка
════════ Exception caught by widgets library ═══════════════════════════════════
Bad state: field does not exist within the DocumentSnapshotPlatform
The relevant error-causing widget was
StreamBuilder<QuerySnapshot<Object?>> StreamBuilder:file:///home/***/Documents/flutter/***/***/lib/mainScreen/bottomNavigation/timeline.dart:145:24
════════════════════════════════════════════════════════════════════════════════