Riverpod's AsyncValue's when method's properties loading: and error: does not work as official document

Riverpod's AsyncValue's when method's properties loading: and error: does not work as official document

根据 https://pub.dev/documentation/riverpod/latest/riverpod/AsyncValue-class.html , 我创建了以下代码;

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

class User {
  final name = '';
}

final userProvider = StreamProvider<User>((_) async* {
  // fetch the user
});

class Example extends ConsumerWidget {
  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final AsyncValue<User> user = ref.watch(userProvider);

    return user.when(
      loading: () => CircularProgressIndicator(), // <---here
      error: (error, stack) => Text('Oops, something unexpected happened'), // <---here
      data: (User user) => Text('Hello ${user.name}'),
    );
  }
}

我在加载时遇到以下错误:和错误:

正在加载:

The argument type 'CircularProgressindicator Function()' can't be assigned to the parameter type 'Widget Function(Async Value?)'. (Documentation)

错误:

The argument type 'Text Function(Object, StackTrace?)' can't be assigned to the parameter type 'Widget Function (Object, StackTrace?, Async Data?)'. (Documentation)

error screen

这是今天突然发生的事情。直到昨天还完全没有问题。

我正在使用 flutter_riverpod-1.00-dev.11.

我已经做了“flutter clean”,删除了pubspec.lock,“flutter pub get”。

你有解决这个问题的办法吗?

最近更新了文档,因为 AsyncValuemaster 分支上发生了更改,但更改尚未发布。

由于您使用的是版本 1.0.0-dev.11,因此语法为:

return user.when(
  loading: (_) => CircularProgressIndicator(), // <---here
  error: (error, stack, _) => Text('Oops, something unexpected happened'), // <---here
  data: (User user) => Text('Hello ${user.name}'),
);

最近删除了那些额外的参数,因此您不应该尝试使用它们。