如何将值传递给另一个类?

时间:2019-06-09 09:02:40

标签: flutter dart

我有一个变量“ phoneUser”,我想传递给另一个类。这两个类都继承自StatefulWidget。 通过构造函数不起作用,另一个类中的变量为空。

void signInWithCredential(String smsCode) async {
    final AuthCredential credential = PhoneAuthProvider.getCredential(
      verificationId: _verificationId,
      smsCode: _smsController.text,);
    final FirebaseUser user = await _auth.signInWithCredential(credential);
    final FirebaseUser currentUser = await _auth.currentUser();
    final String phoneUser = currentUser.phoneNumber;// I wanna pass phoneUser to another class 

    setState(() {/.../}});
class Approval extends StatefulWidget {
  final String timePicked;
  final DateTime pickedDay;
  final String userPhone;
  Approval({Key key, @required DateTime picked, @required String timePicked,
    @required String phoneNumber})
      : pickedDay = picked, timePicked = timePicked, userPhone = phoneNumber,
        super(key: key);
  _ApprovalState createState() => new _ApprovalState(pickedDay,timePicked,userPhone);
}
class _ApprovalState extends State<Approval> {
  DatabaseReference itemRef = FirebaseDatabase.instance.reference().child('customers');
  final DateTime pickedDay;
  final String timePicked;
  final String userPhone;
  _ApprovalState(this.pickedDay, this.timePicked, this.userPhone);
////userPhone is empty here
/// This constructor also does not work:
class Amenities extends StatefulWidget {
  final String phoneNumber;
  Amenities({Key key, this.phoneNumber}):super(key: key);
  AmenitiesState createState() => new AmenitiesState();
}
Text(widget.phoneNumber) //phoneNumber is still empty

0 个答案:

没有答案