【发布时间】:2020-08-31 08:57:20
【问题描述】:
我想在我的设备上配置返回按钮, 我有问题未定义的“Navigator.pop(context);”在这段代码中,任何人都可以对我进行编辑。谢谢
import 'dart:js';
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
void backButton() {
Navigator.pop(context);
}
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () {
backButton();
return Future.value(false);
},
child: MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Welcome to Flutter',
home: Scaffold(
body: WebView(
initialUrl: "https://halgoom.com",
javascriptMode: JavascriptMode.unrestricted,
),
),
),
);
}
}
【问题讨论】:
-
我认为
context没有在你调用它的地方提供。(我的意思是 backButton() 函数) -
您使用
StatelessWidget,它没有自己的类作用域context。您应该将context作为backButton函数的参数传递,或者在onWillPop回调中调用Navigator.pop。 -
@Prostandy 如果答案解决了您的问题。请记住将其标记为正确,以帮助将来遇到相同问题的用户。