简体   繁体   中英

NoSuchMethodError: The method 'call' was called on null

I try to implement firebase auth anonymous and i am getting the following error:

I/flutter (11251): NoSuchMethodError: The method 'call' was called on null. I/flutter (11251): Receiver: null I/flutter (11251): Tried calling: call(Instance of 'User')

pubspec.yaml:

dependencies:
  flutter:
    sdk: flutter
  animated_text_kit: ^4.2.1
  cupertino_icons: ^1.0.2
  firebase_core: 0.5.1
  firebase_auth: 0.18.2

Part of my main.dart:

...
Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: LandingPage(), // SplashScreen
    ); // define it once at root level.
  }
}
...
...
...


class ExcRoute extends StatelessWidget {
  // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  const Exc({Key key, @required this.onSignIn}) : super(key: key);
  final void Function(User) onSignIn;

  Future<void> _signInAnonymously() async {
    try {
      final userCredentials = await FirebaseAuth.instance.signInAnonymously();
      onSignIn(userCredentials.user);
    } catch (e) {
      print(e.toString());
    }
  }

  // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(

my landing_page.dart:


import 'package:exc/main.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';

class LandingPage extends StatefulWidget {
  // const LandingPage({Key? key}) : super(key: key);

  @override
  _LandingPageState createState() => _LandingPageState();
}

class _LandingPageState extends State<LandingPage> {
  User _user;

  void _updateUser(User user) {
    setState(() {
      _user = user;
    });
  }

  @override
  Widget build(BuildContext context) {
    if (_user == null) {
      return ExcRoute(
        onSignIn: _updateUser,
      );
    }
    return Container();
  }
}```

The error NoSuchMethodError: The method 'call' was called on null. points to User. It's likely that you're trying to call an instance of the User object even if it's null. You may want to check if FirebaseAuth.instance.signInAnonymously(); successfully logs-in. It's likely that the error occurs on this part. A simple null check should be a good workaround for this issue.

final userCredentials = await FirebaseAuth.instance.signInAnonymously();
if(userCredentials != null) {
  onSignIn(userCredentials.user);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM