0

I'm trying to use Firebase to login, but I'm getting Type 'Null' is not a sub type of string as error when I try to parse _userId = responseData['localId'];

When I try to parse _token = responseData['idToken'];, it works correctly.

responseData['localId'] is not NULL.

Here is full code:

class Auth with ChangeNotifier {
String? _token;
String? _userId;

Future<void> _authenticate(
  String email, String password, String urlSegment) async {
  var url = Uri.parse(
    'https://www.google.com');
  var body = json.encode({
  'email': email,
  'password': password,
  'returnSecureToken': true,
 });
 try {
   final response = await http.post(url, body: body);
   final responseData = json.decode(response.body);
   _token = responseData['idToken'];
   _userId = responseData['localId'];
   } catch (err) {
     throw err;
   }
}

  Future<void> login(String email, String password) async {
    return _authenticate(email, password, 'signInWithPassword');
  }
}
munsra
  • 9
  • 2
  • What's the output of `print(response.body);`? – Repox Jan 14 '22 at 09:43
  • { "kind": "identitytoolkit#VerifyPasswordResponse", "localId": "q2oyit2Tf6aUkM0dO9huZlPyx9H3", "email": "test@test.com", "displayName": "", "idToken": "eyJhbGciOiJSUz", "registered": true, "refreshToken": "AFxQ4_rv1LlH2A", "expiresIn": "3600" } – munsra Jan 14 '22 at 09:53
  • Can you please add the entire error message? – Jahn E. Jan 14 '22 at 09:58
  • _TypeError (type 'Null' is not a subtype of type 'String') – munsra Jan 14 '22 at 10:10
  • I think your error is caused by something else. Can you try, for the sake of it, to remove the `try { } catch()` block and paste the entire error message (including offending line etc) – Repox Jan 14 '22 at 10:13

0 Answers0