felangel / fresh

🍋 A token refresh library for Dart.
https://github.com/felangel/fresh
360 stars 57 forks source link

token returns null when awaited right after initializing #70

Closed jteague9 closed 4 months ago

jteague9 commented 2 years ago

Code to reproduce:

import 'package:fresh/fresh.dart';

class InMemoryDelayed extends InMemoryTokenStorage<String> {
  @override
  Future<String?> read() async {
    await Future<dynamic>.delayed(Duration.zero);
    return super.read();
  }
}

class Foo with FreshMixin<String> {
  Foo(InMemoryDelayed tokenStorage) {
    this.tokenStorage = tokenStorage;
  }
}

void main() async {
  var storage = InMemoryDelayed();
  await storage.write('token');
  var foo = Foo(storage);
  var token = await foo.token;
  print(token); // prints null, but should print 'token'
}

The token method is not waiting for the first read from storage to complete.

https://github.com/felangel/fresh/blob/master/packages/fresh/lib/src/fresh.dart#L110 this line can be changed to await authenticationStatus.firstWhere((status) => status != AuthenticationStatus.initial);

I can put up a PR if you think this fix is appropriate.

lyio commented 6 months ago

I ran into the same problem yesterday.

vasilich6107 commented 5 months ago

Hey @felangel Could you take a look at the issue and PR? Probably it's just matter of pressing of merge button

felangel commented 4 months ago

Closed by #96