【发布时间】:2020-08-03 04:49:09
【问题描述】:
我正在尝试从将来的方法导航到屏幕。但是我收到一个错误,说未定义的名称上下文。我尝试从 Widget build 导航,但参数是在此方法中创建的,我需要它来导航。我已经坚持了很长时间。任何帮助将不胜感激。
Future<void> addBookingConversation(Booking booking) async {
Conversation conversation = Conversation();
await conversation.addConversationToFirestore(booking.posting.host); //additional method working fine
String text = "Hi, my name is ${AppConstants.currentUser.firstName}";
await conversation.addMessageToFirestore(text); //additional method working fine
//this is where i should navigate to the conversation page and facing the error here
Navigator.push(
context, //error here context undefined
MaterialPageRoute(builder:
(context) => ConversationPage(conversation: conversation,),
),
);
}
class ConversationPage extends StatefulWidget {
final Conversation conversation;
static final String routeName = '/conversationPageRoute';
ConversationPage({this.conversation, Key key}) : super(key: key);
@override
_ConversationPageState createState() => _ConversationPageState();
}
class _ConversationPageState extends State<ConversationPage> {
Conversation _conversation;
// additional code of wiget build
}
【问题讨论】:
-
把你的
addBookingConversation函数放在Widget build(BuildContext context)下面,其中context是定义的。
标签: flutter dart flutter-navigation