1
@override
  Widget build(BuildContext context) {

    final user = Provider.of<User>(context);

    return StreamProvider<QuerySnapshot>.value(   // irrelevant (?)
      value: DatabaseService().users,
      child: StreamBuilder<UserData>(
        stream: DatabaseService(uid: user.uid).userData,
        builder: (context, snapshot) {
          UserData userData = snapshot.data;
          if (!snapshot.hasData) {
            return ProfileSetup();
          } else {
            return Scaffold(...

I want to check if the user has already set up his profile by checking if there is any data of the user in the Firestore Cloud. Otherwise the user gets send to the setup page. The problem is that when there is finally the user data in the cloud I get an error message and a red screen for half a second, but after that it continues as it's suppost to. Yet I don't understand why there is an error message.

A build function returned null. The relevant error-causing widget was StreamBuilder< UserData>

2 Answers 2

1

Use connectionstates in streambuilders. Try this:

@override
Widget build(BuildContext context) {

  final user = Provider.of<User>(context);

return StreamProvider<QuerySnapshot>.value(   // irrelevant (?)
   value: DatabaseService().users,
   child: StreamBuilder<UserData>(
   stream: DatabaseService(uid: user.uid).userData,
      builder: (context, snapshot) {
       UserData userData = snapshot.data;
       if (snapshot.hasData) {
       switch (snapshot.connectionState) {
       case ConnectionState.none:
                 return Text("No Connections");
       case ConnectionState.waiting:
                return CircularProgressIndicator();
       case ConnectionState.active:
       case ConnectionState.done:
         return snapshot.data.length > 0 ? ScaffoldPage() :ProfileSetup();
    default:
    break;
  }
  } 
          return Text("");
Sign up to request clarification or add additional context in comments.

5 Comments

Unfortunately I still get the error. Also, (context, snapshot) is now underlined and says: This function has a return type of 'Widget', but doesn't end with a return statement. Try adding a return statement, or changing the return type to 'void'.
It means, you forgot to return widget for the builder function in some switch case @Vinzent
I've edited my answer try this now. Replace else condition with return Text("") or SizedBox()
I just realised that I had else { return null; } at the next StreamBuilder and that was why I got the error... thank's a lot anyway!
But using the connection state helped me now to get rid of another problem so thank you :)
1

I am using firestore in my new project and has same problem

In my case I wasn't getting any error but snapshot has no data so I did checked

if (snapshot.hasError) return Text('error ${snapshot.error}');

which helped me see what's error and it was because index wasn't enable

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.