0

I have a variable 'phoneUser' that I want to pass to another class. Both classes are inherited from StatefulWidget. Passing through constructor does not work, the variable in the other class is empty.

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

1 Answers1

0

Passing a value to a class can be done using constructors.

The reason why you're getting the error "A non-null String must be provided to a Text widget" is because you're passing a nullable value. Adding a null-check or defining a default value should solve this issue.

Omatt
  • 8,564
  • 2
  • 42
  • 144