简体   繁体   中英

Flutter StreamBuilder returned null

@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>

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("");

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM