Ad

I Have Migrated To Null-safety (2.15.1) And This Problem Still Persists

- 1 answer

I get the error below when I compile the app.

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>')

The error points me to the 'MyApp' part of the code and so I have no idea how to tackle this one. My app ran with no error before migrating. This is a part of my code where the cause for this error is. I went through the code with and I can't find a possible syntax error.

    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);

    ```
Ad

Answer

Riverpod removed useProvider in version 1.0.0. As described in the migration guide, you will need to use StatefulHookConsumerWidget instead of StatefulHookWidget to access that same functionality in the newest version:

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

class _MyAppState extends ConsumerState<MyApp> {

  // ...

  @override
  Widget build(BuildContext context) {
    userLoggedIn = ref.watch(userLoggedInProvider);
    // ...
Ad
source: stackoverflow.com
Ad