【问题标题】:How can I solve "NoSuchMethodError (NoSuchMethodError: The setter 'uid=' was called on null"如何解决“NoSuchMethodError (NoSuchMethodError: setter 'uid=' was called on null”)
【发布时间】:2021-08-03 20:05:25
【问题描述】:

我是新的 ChangeNotifier 提供者颤振。我正在尝试从 firebase 获取用户 ID。我想从firebase查询当前用户的数据。但是当我运行我的应用程序时,就会发生错误。

NoSuchMethodError (NoSuchMethodError: The setter 'uid=' was called on null.
Receiver: null
Tried calling: uid="AFdLhdPxWmWZ5D1HjttjQvrvKX42")

我不知道我的代码有什么问题以及如何解决这个问题。

这是我遇到问题的代码。

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  final Completer<GoogleMapController> _mapController = Completer();
  Customer _currentCustomer;

  @override
  void initState() {
    AuthNotifier authNotifier =
        Provider.of<AuthNotifier>(context, listen: false);
    if (authNotifier.currentCustomer != null) {
      _currentCustomer = authNotifier.currentCustomer;
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    AuthNotifier authNotifier = Provider.of<AuthNotifier>(context);

    return Scaffold(
        appBar: AppBar(
          title: Text(_currentCustomer.uid),
        ),
        body: Container(
            child: Column(children: [
          Flexible(flex: 2, child: MapWidget(mapController: _mapController)),
          SizedBox(height: 12),
          Flexible(
              flex: 2, child: StoreListWidget(mapController: _mapController)),
          SizedBox(height: 12),
          FloatingActionButton.extended(
            onPressed: () {
              Navigator.push(
                  context,
                  MaterialPageRoute(
                      builder: (context) =>
                          SelectMap(uid: _currentCustomer.uid)));
            },
            label: Text('Get Current Location'),
            icon: Icon(Icons.directions_boat),
          )
        ])));
  }
}

这是 ChangeNotifier 类。

class AuthNotifier with ChangeNotifier {
  User _user;
  Customer _currentCustomer;

  User get user => _user;
  Customer get currentCustomer => _currentCustomer;

  void setUser(User user) {
    _user = user;
    notifyListeners();
  }

  void setCurrentCustomer(String uid) {
    _currentCustomer.uid = uid;
    notifyListeners();
  }
}

这是验证码。

login(Customer customer, AuthNotifier authNotifier) async {
  BuildContext context;
  UserCredential authResult = await FirebaseAuth.instance
      .signInWithEmailAndPassword(
          email: customer.email, password: customer.password)
      .catchError((error) => print(error));

  if (authResult != null) {
    User firebaseUser = authResult.user;

    if (firebaseUser != null) {
      print('Login with: $firebaseUser');
      authNotifier.setUser(firebaseUser);
      authNotifier.setCurrentCustomer(firebaseUser.uid);
    }
  }
}

register(Customer customer, AuthNotifier authNotifier) async {
  UserCredential authResult = await FirebaseAuth.instance
      .createUserWithEmailAndPassword(
          email: customer.email, password: customer.password)
      .catchError((error) => print(error));

  if (authResult != null) {
    User firebaseUser = authResult.user;

    if (firebaseUser != null) {
      BuildContext context;
      FirebaseFirestore.instance.collection('users').doc(firebaseUser.uid).set({
        'uid': firebaseUser.uid,
        'userName': customer.userName,
        'email': customer.email,
      })
      .catchError((error) => print("Failed to add user: $error"));
      User currentUser = await FirebaseAuth.instance.currentUser;
      authNotifier.setUser(currentUser);
      authNotifier.setCurrentCustomer(firebaseUser.uid);
    }
  }
}

initializeCurrentUser(AuthNotifier authNotifier) async {
  User firebaseUser = await FirebaseAuth.instance.currentUser;

  if (firebaseUser != null) {
    print(firebaseUser);
    authNotifier.setUser(firebaseUser);
    authNotifier.setCurrentCustomer(firebaseUser.uid);
  }
}

【问题讨论】:

    标签: firebase flutter dart flutter-change-notifier


    【解决方案1】:

    在您的 AuthNotifier 类中,您似乎没有初始化 _currentCustomer

    class AuthNotifier with ChangeNotifier {
        User _user;
        Customer _currentCustomer; // <---- No where initialised
    
        User get user => _user;
        Customer get currentCustomer => _currentCustomer;
    
        void setUser(User user) {
          _user = user;
          notifyListeners();
        }
    
        void setCurrentCustomer(String uid) {
          _currentCustomer.uid = uid;
          notifyListeners();
        }
    }
    

    所以在_currentCustomer.uid = uid; 中,_currentCustomer 将为空。

    如果可能,我还建议您将代码迁移到Sound Null Safety。它可以帮助您在编译时避免这些问题。

    【讨论】:

      猜你喜欢
      • 2023-03-23
      • 1970-01-01
      • 2021-12-31
      • 1970-01-01
      • 2019-12-05
      • 1970-01-01
      • 2020-06-26
      • 2018-11-08
      • 2019-12-06
      相关资源
      最近更新 更多