Null check operator used on a null value on Flutter 2.5.3

Solution 1:

Problem:

This issue was actually caused by the null check operator (!) added to the non-nullable String variable auth.token in your main.dart file, which basically promises Dart that the value of auth.token would never be null, and that promise was not kept because the _token variable in the logout() method in your auth.dart file was set to null and your _token is actually passed to your token getter that you call using auth.token! in your main.dart file.

Solution:

You can of course easily fix this by removing the null check operator from the auth.token variable in your main.dart file and setting it to an empty String if it's value was equal to null, like this:

ChangeNotifierProxyProvider<Auth, Products>(
       create: (_) => Products('', '', []),
       update: (ctx, auth, previousProducts) => Products(
         auth.token ?? '',
         auth.userId ?? '',
         previousProducts == null ? [] : previousProducts.items,
       ),
     ),

you should also make your userId getter nullable like this or it would throw another error:

String? get userId {
  return _userId;
}

the ?? operator in Dart is basically an assignment operator that executes when the variable it is used for is equal to null.

And here's an extra guide from the Dart Team on working with null safety: https://dart.dev/null-safety