I have a little issue with a snapshot and a GridView. I have used GridView because I am making a notes management app. Here is an error that is shown in the console:-
The following StateError was thrown building StreamBuilder<QuerySnapshot<Object?>>(dirty, state: _StreamBuilderBaseState<QuerySnapshot<Object?>, AsyncSnapshot<QuerySnapshot<Object?>>>#fefb4): Bad state: field does not exist within the DocumentSnapshotPlatform
I do not understand how to fix the problem. I have checked the name of the fields in the document and it is fine. Please, can you help me to understand this? It would be appreciated. Thank you...!!!
// ignore_for_file: prefer_const_constructors
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/foundation/key.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:notes_app/Screens/note_add.dart';
import 'package:notes_app/Screens/note_read.dart';
import 'package:notes_app/Utils/utils.dart';
import 'package:notes_app/Widgets/notes_card.dart';
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
final height = MediaQuery.of(context).size.height;
final width = MediaQuery.of(context).size.width;
return Scaffold(
backgroundColor: AppStyle.mainColor,
appBar: AppBar(
elevation: 0,
title: Text('Notes'),
centerTitle: true,
backgroundColor: AppStyle.mainColor,
),
body: Padding(
padding: const EdgeInsets.all(20),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Your recent Notes',
style: GoogleFonts.roboto(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 22,
),
),
SizedBox(
height: height * 0.02,
),
Expanded(
child: StreamBuilder<QuerySnapshot>(
stream:
FirebaseFirestore.instance.collection('Notes').snapshots(),
builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(child: CircularProgressIndicator());
}
if (snapshot.hasData) {
return GridView(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
crossAxisSpacing: 4,
mainAxisSpacing: 4,
),
children: snapshot.data!.docs
.map((note) => noteCard(() {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ReadPage(note)));
}, note))
.toList(),
);
}
return Text(
'There is no Notes',
style: GoogleFonts.nunito(
color: Colors.white,
),
);
},
),
)
],
),
),
floatingActionButton: FloatingActionButton.extended(
onPressed: () {
Navigator.push(
context, MaterialPageRoute(builder: (context) => AddPage()));
},
label: Text('Add Note'),
icon: Icon(Icons.add),
),
);
}
}