flutter Riverpod Generator应该生成一个流,但它生成了一个异步值

yc0p9oo0  于 2023-08-07  发布在  Flutter
关注(0)|答案(1)|浏览(150)

我已经在之前的应用程序行中为用户流做了这个确切的例子。g.dart generator生成了一个我可以使用的流,但这次它没有生成流:
userStream.dart:

part 'userstream.g.dart';

@riverpod
class Users extends _$Users {
  @override
  Stream<User?> build() {
    return FirebaseAuth.instance.authStateChanges();
  }
}

字符串
它生成了这个userstream.g.dart:

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'userstream.dart';

// **************************************************************************
// RiverpodGenerator
// **************************************************************************

String _$usersHash() => r'7d9a531b75593cd952a52ca555796bde5a421750';

/// See also [Users].
@ProviderFor(Users)
final usersProvider = AutoDisposeStreamNotifierProvider<Users, User?>.internal(
  Users.new,
  name: r'usersProvider',
  debugGetCreateSourceHash:
      const bool.fromEnvironment('dart.vm.product') ? null : _$usersHash,
  dependencies: null,
  allTransitiveDependencies: null,
);

typedef _$Users = AutoDisposeStreamNotifier<User?>;
// ignore_for_file: type=lint
// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member


所以当我尝试使用流时,它不会工作,它说:
参数类型'AsyncValue<User?>'无法分配给参数类型'Stream<User?>?'

Widget build(BuildContext context, WidgetRef ref) {
    var users = ref.watch(usersProvider);
    return Scaffold(
      //Streams can always be put in a streamProvider
      body: StreamBuilder<User?>(
          //if has to be nullable becasue if the user is not signed in its null
          stream: users, //


上一次我做了这个精确的登录行,userstream.g.dart看起来是这样的:

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'userstream.dart';

subtype_of_sealed_class, invalid_use_of_internal_member, unused_element, constant_identifier_names, unnecessary_raw_strings, library_private_types_in_public_api

/// Copied from Dart SDK
class _SystemHash {
  _SystemHash._();

  static int combine(int hash, int value) {
    // ignore: parameter_assignments
    hash = 0x1fffffff & (hash + value);
    // ignore: parameter_assignments
    hash = 0x1fffffff & (hash + ((0x0007ffff & hash) << 10));
    return hash ^ (hash >> 6);
  }

  static int finish(int hash) {
    // ignore: parameter_assignments
    hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3));
    // ignore: parameter_assignments
    hash = hash ^ (hash >> 11);
    return 0x1fffffff & (hash + ((0x00003fff & hash) << 15));
  }
}

String _$UsersHash() => r'7d9a531b75593cd952a52ca555796bde5a421750';

/// See also [Users].
final usersProvider = AutoDisposeNotifierProvider<Users, Stream<User?>>(
  Users.new,
  name: r'usersProvider',
  debugGetCreateSourceHash:
      const bool.fromEnvironment('dart.vm.product') ? null : _$UsersHash,
);
typedef UsersRef = AutoDisposeNotifierProviderRef<Stream<User?>>;

abstract class _$Users extends AutoDisposeNotifier<Stream<User?>> {
  @override
  Stream<User?> build();
}


是不是新的水荚发电机出了什么问题?

ddrv8njm

ddrv8njm1#

var users = ref.watch(usersProvider);将具有AsyncValue<User?>作为其值。那不是溪流。StreamProvider或StreamNotifierProvider在观看或读取或收听时始终生成更新的AsyncValue,* 与FutureProvider或AsyncNotifierProvider相同 *。两者之间唯一的区别是build()的返回值(Stream vs Future)。除此之外,这两个async家族的工作方式完全相同。

相关问题