I try to use StreamBuilder and I handle error by throw snapshot.error
StreamBuilder(
stream: stream,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.active) {
if (snapshot.hasError) {
throw snapshot.error!; <- This line
}
}
},
);
But It tell that This line is the one making error.
Is there any way to see where actual error come from?
Related
I am trying to log in with two different types on flutter firebase. I tried too many things but still, I couldn't do it. In the end, I found another solution but I got new errors.
Bad state: cannot get a field on a DocumentSnapshotPlatform that does not exist.
StreamBuilder<User?>(
stream: FirebaseAuth.instance.authStateChanges(),
builder: (context, snapshot) {
if(snapshot.hasData && snapshot.data != null) {
return StreamBuilder<DocumentSnapshot>(
stream: FirebaseFirestore.instance.collection("users").doc(snapshot.data!.uid).snapshots() ,
builder: (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot){
if(snapshot.hasData && snapshot.data != null) {
final userDoc = snapshot.requireData;
// final user = userDoc.data();
if(userDoc.get('role') == 'admin') {
return AdminPage();
}else{
return Home();
}
}else{
return Material(
child: Center(child: CircularProgressIndicator(),),
);
}
},
);
}
return LoginPage();
}
);
I'm doing an app with two logins. Patients and doctors. Doctors can control patients or smth like that. So doctors should be admins. I made two login options. Admin login and user login. But if I can do it on the same login page, it would be good.
When a screen where the main content comes from an API, what would be the option to have a good UI? Should this really be done with FutureBuilder?
Yes, you can use FutureBuilder and use different states to inform your users.
For example:
return FutureBuilder(
future: _getMark(context),
builder: (ctx, snapshot) {
if (snapshot.hasData) {
return snapshot.data as Widget;
}
if (snapshot.hasError) {
//show error
}
return markUnRead;
},
);
I'm new to flutter and have a array in firestore. It's structure is:
Now i want to fetch that array, and use that array to build a dropdownFormfield.
My fetch Function is following:
fetchSchoolList() async {
List<dynamic> data = [];
Future<DocumentSnapshot<Object?>> result = schoolList.doc('List').get();
return result;
}
and my FutureBuilder is:
FutureBuilder<DocumentSnapshot<Object?>>(
future: fetchSchoolList(),
builder: (BuildContext context,
AsyncSnapshot<DocumentSnapshot> snapshot) {
if (snapshot.connectionState ==
ConnectionState.active) {
return CircularProgressIndicator();
}
if (snapshot.hasError) {
return Text("Something went wrong");
}
if (snapshot.connectionState == ConnectionState.done) {
print(snapshot);
}
return Text('Loading List');
}),
But with this i'm even unable to get that array to print. I am getting error:
type 'Future' is not a subtype of type 'Future<DocumentSnapshot<Object?>>?'
Please help, i'm stuck here for days.#
There's a couple of things you could do:
First of all change fetchSchoolList() async {...} to something like this:
Future<DocumentSnapshot<List<String>?>>() async {
Future<DocumentSnapshot<List<String>?>> result =schoolList.doc('List').get();
return result;
}
Object isn't exactly a type, also flutter doesn't really know how to store information in with the type <Object>. Also you don't reference the data variable anywhere so it has been omitted.
Next change your FutureBuilder to something like this:
FutureBuilder<DocumentSnapshot<List<String>?>>(
future: fetchSchoolList(),
builder: (context, snapshot) {
if (snapshot.connectionState ==
ConnectionState.active) {
return CircularProgressIndicator();
}
if (snapshot.hasError) {
return Text("Something went wrong");
}
if (snapshot.connectionState == ConnectionState.done) {
print(snapshot);
}
return Text('Loading List');
}),
This will specify what is being returned by the future (Basically the FutureBuilder's type) and because of that, you do not need to specify the snapshot type.
I have found some posts that address this issue and I have tried to use their solutions without success.
I am new to flutter so I need someone's advice on what to do here.
Here is the error message in the Debug panel:
A build function returned null.
The offending widget is: StreamBuilder<List<Event>>
Build functions must never return null.
To return an empty space that causes the building widget to fill available room, return "Container()". To return an empty space that takes as little room as possible, return "Container(width: 0.0, height: 0.0)".
The relevant error-causing widget was:
StreamBuilder<List<Event>> file:///C:/Users/nkane/AndroidStudioProjects/tonnah/lib/screens/appointment_calendar.dart:166:11
Here is the code starting at line 166:
StreamBuilder(
//stream: _firestoreService.getEventStream(_selectedDay),
stream: _db.collection('agency').doc(globals.agencyId).collection('event')
.where('eventDate', isGreaterThanOrEqualTo: Timestamp.fromDate(_selectedDay))
.snapshots().map((snapshot) => snapshot.docs
.map((document) => Event.fromFirestore(document.data()))
.toList()),
builder: (context, snapshot) {
if (snapshot.hasData) {
List<Event> snapData;
return snapData = snapshot.data;
_eventsStream = snapData;
_eventsMap = convertToMap(snapData);
//_selectedEventsMap = _eventsMap[_selectedDay] ?? [];
return _buildTableCalendar();
}
},
),
How do I fix this? I know I need a "return" but how do I use it?
You just need to return a view while the data is fetching/loading. In this case I've used a CircularProgressIndicator widget.
StreamBuilder(
stream: ...,
builder: (context, snapshot) {
if(snapshot.hasData){
return build_your_widget;
} else
return CircularProgressIndicator();
}
)
StreamBuilder(
stream: FirebaseFirestore.instance.collection('users').snapshots(),
builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (!snapshot.hasData) {
return CircularProgressIndicator();
}
FirestoreUser firestoreUser =
FirestoreUser.fromDocument(snapshot.data); // Here snapshot.data is retrieving QuerySnapshot.
// How I can convert it to DocumentSnapshot
...
Hello StackOverflow users
I need to give to my new firestoreUser variable, which type of DocumentSnapshot. But I can't get it.
After writing snapshot.data it gives me an error called "QuerySnapshot is not subtype of DocumentSnapshot"
P.s I'm using StreamBuilder as you can see. Thank you
Your stream is FirebaseFirestore.instance.collection('users').snapshots() which is a QuerySnapshot, meaning, a List of QueryDocumentSnapshot which Extends DocumentSnapshot.
So if you want the documentSnapshot of every users in your 'users' collection, you will have to iterate over snapshot.data.docs:
But if you want to get the document of a particular user, then you can do:
StreamBuilder(
stream: FirebaseFirestore.instance.collection('users').doc(userID).snapshots(),
builder: (context, AsyncSnapshot<DocumentSnapshot> snapshot) {
if (!snapshot.hasData) {
return CircularProgressIndicator();
}
FirestoreUser firestoreUser =
FirestoreUser.fromDocument(snapshot.data);
...