我已经迁移到 null-safety (2.15.1),这个问题仍然存在

I have migrated to null-safety (2.15.1) and this problem still persists

我在编译应用程序时遇到以下错误。

The following NoSuchMethodError was thrown building MyApp(dirty, state: _MyAppState#73713):
The method 'call' was called on null.
Receiver: null
Tried calling: call(Instance of 'ChangeNotifierProvider<UserLoggedIn>')

错误将我指向代码的 'MyApp' 部分,因此我不知道如何解决这一问题。 我的应用程序 运行 在迁移之前没有错误。 这是我的代码的一部分,导致此错误的原因。 我检查了代码,但找不到可能的语法错误。

    void main() {
  runApp(ProviderScope(child: MyApp()));
}

class MyApp extends StatefulHookWidget {
   @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {


  bool _amplifyConfigured = false;
  bool checkAuthStatus = false;

  late AmplifyAuthCognito auth;


  var userLoggedIn;
  var useProvider;

  @override

  void initState() {
    // TODO: implement initState
    super.initState();
    _configureAmplify();
  }

  void _configureAmplify() async {
    if (!mounted) return;


    auth = AmplifyAuthCognito();
    await Amplify.addPlugin(auth);

    try {
      await Amplify.configure(amplifyconfig);
    } on AmplifyAlreadyConfiguredException {
      print('Already configured');
    }

    try {
      getUserStatus();
      setState(() {
        _amplifyConfigured = true;
      });
    } catch (e) {
      print(e);
    }
  }



  @override
  Widget build(BuildContext context) {
    userLoggedIn = useProvider(userLoggedInProvider);

    ```

Riverpod 在版本 1.0.0 中删除了 useProvider。如 migration guide 中所述,您将需要使用 StatefulHookConsumerWidget 而不是 StatefulHookWidget 来访问最新版本中的相同功能:

class MyApp extends StatefulHookConsumerWidget {
   @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends ConsumerState<MyApp> {

  // ...

  @override
  Widget build(BuildContext context) {
    userLoggedIn = ref.watch(userLoggedInProvider);
    // ...