'I have error on context..The argument type 'Context' can't be assigned to the parameter type 'BuildContext'
postDetailsToFirestore() async {
FirebaseFirestore firebaseFirestore = FirebaseFirestore.instance;
User? user = _auth.currentUser;
UserModel userModel = UserModel();
// writing all the values
userModel.email = user!.email;
userModel.uid = user.uid;
userModel.name = NameEditingController.text;
await firebaseFirestore
.collection("users")
.doc(user.uid)
.set(userModel.toMap());
Fluttertoast.showToast(msg: "Account created successfully :) ");
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(builder: (BuildContext context) => HomeScreen()),
(route) => false);
}
}
I got an error in the context, I have reviewed and already put import 'package:path/path.dart'; but still same error occur.
Solution 1:[1]
You are trying to push a new route using the Navigator. This requires a buildContext, which you don't have in this function but reference in the Navigator.pushAndRemoveUntil function, and are therefore required to pass into the function when you call it.
Update your function declaration to accept a buildContext:
postDetailsToFirestore(BuildContext context) async {
//Rest of your function body here
}
Then when you call the function, assuming you are calling it in a widget where buildContext exists, pass in the context:
postDetailsToFirestore(context);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Noah |
