The parameter 'firebaseFirestore' can't have a value of 'null' because of its type, but the implicit default value is 'null' in flutter

1,052

To guarantee that you never see a null parameter with a non-nullable type, the type checker requires all optional parameters to either have a nullable type or a default value.

Understanding null safety | Dart

You need to make firebaseFirestore and firebaseAuth nullable since they are optional and their default values are not constant.

You can do that by adding ? right after the type.

Like this:

    FirebaseFirestore? firebaseFirestore,
    auth.FirebaseAuth? firebaseAuth,

So your constructor can be updated to this:

    AuthRepository({
        FirebaseFirestore? firebaseFirestore,
        auth.FirebaseAuth? firebaseAuth,
      })  : _firebaseFirestore = firebaseFirestore ?? FirebaseFirestore.instance,
            _firebaseAuth = firebaseAuth ?? auth.FirebaseAuth.instance;
Share:
1,052
Shreyansh Sharma
Author by

Shreyansh Sharma

Updated on December 22, 2022

Comments

  • Shreyansh Sharma
    Shreyansh Sharma over 1 year

    I declared two variables of type FirebaseFirestore and FireBaseAuth, and now i am facing an error of null safety. I am using a constructor in my class, and passing them as paraeters, with getting an error of - The parameter 'firebaseFirestore' can't have a value of 'null' because of its type, but the implicit default value is 'null' in flutter and The parameter 'firebaseAuth' can't have a value of 'null' because of its type, but the implicit default value is 'null' in flutter.

    The code part consisting of my problem is -

    import 'package:flutter_instagram_clone_final/repositories/repositories.dart';
    import 'package:firebase_auth/firebase_auth.dart' as auth;
    
    class AuthRepository extends BaseAuthRepository {
    
      final FirebaseFirestore _firebaseFirestore;
      final auth.FirebaseAuth _firebaseAuth;
    
      AuthRepository({
        FirebaseFirestore firebaseFirestore,
        auth.FirebaseAuth firebaseAuth,
      })  : _firebaseFirestore = firebaseFirestore ?? FirebaseFirestore.instance,
            _firebaseAuth = firebaseAuth ?? auth.FirebaseAuth.instance;
    

    Then I tried to initialize it with instance, like -

        FirebaseFirestore firebaseFirestore = FirebaseFirestore.instance,
    

    But it is giving me an error that - The default value of an optional parameter must be constant.

  • Shreyansh Sharma
    Shreyansh Sharma almost 3 years
    Thanks bro. So generous and kind of you for helping me out on this small error.