Home > database >  Flutter Error: StreamProvider requires initialData parameter after null-safety added. StreamProvider
Flutter Error: StreamProvider requires initialData parameter after null-safety added. StreamProvider

Time:04-26

since the Flutter null-safety update, my old code was broken. I managed to fix most of it, but this bit is still unresolved.

Here is the code first:

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return StreamProvider<MyUser>.value( //initialData required here now
      value: AuthService().user,
      child: MaterialApp(
        home: Wrapper(),
      ),
    );
  }
}

This code used to work, but now it requires that initialData is passed as a parameter for the StreamProvider. My "MyUser" class is the following:

// Create custom User Model to get only the uid
class MyUser {
  final String uid;

  MyUser({required this.uid});
}

My question is just that I am unsure on what should be my initialData on this case. My AuthService class is basically a class with functions like "sign in with email and password", "register with email and password", etc, all from Firebase. Like this:

// Create user object based on FirebaseUser
  MyUser _userFromFirebaseUser(User? user) {
    return MyUser(uid: user!.uid);
  }

  // Auth change user stream
  Stream<MyUser> get user {
    return _firebaseAuth.authStateChanges().map(_userFromFirebaseUser);
  }

Thank you so much for the help!

CodePudding user response:

make it nullable by adding ? after MyUser and then you can set your initialData to null

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return StreamProvider<MyUser?>.value( 
      initialData: null
      value: AuthService().user,
      child: MaterialApp(
        home: Wrapper(),
      ),
    );
  }
}
  • Related