繁体   English   中英

Flutter使用继承的窗口小部件和导航器返回NoSuchMethodError

[英]Flutter using inherited widgets with Navigator returning NoSuchMethodError

我正在制作一个扑扑的应用程序。 我有一个主页小部件,其中显示了两件事-设备代码; 温度首先,将它们设置为一些默认值,但是用户随后从主页转到新路线,将此小部件称为传感器小部件。 在这个新页面中,它基本上连接到传感器。 该传感器发出设备代码和温度,我可以在传感器小部件中显示它。

当我想在主页上显示此新信息时,问题就来了。 我做了一个按钮,用户可以返回首页,但是我想用我的传感器小部件中的值更新首页。

我正在利用InheritedWidget类来实现此目的,但是当我尝试访问主页中的变量时,我总是收到null错误。

下面是为此继承的窗口小部件类。

class TemperatureContext extends InheritedWidget {
    final int _deviceCode;
    final double _temperature;

    int    get deviceCode => _deviceCode;
    double get temperature => _temperature;

    set deviceCode(int d) {_deviceCode = d;}
    set temperature(double t) {_temperature = t}

    TemperatureContext(this.deviceCode, this.temperature, {Key key, Widget child})
    .super(key: key, child:child)

    @override
    bool updateShouldNotify(Widget oldWidget) {
        return (temperature != oldWidget.temperature && deviceCode != oldWidget.deviceCode) }

    static TemperatureContext of(BuildContext context) {
        return context.inheritFromWidgetOfExactType(TemperatureContext) }
    }

然后,我有了主页,new_widget是一个函数,可基于

    class HomePage extends StatefulWidget {
      @override
      _HomePageState createState() => _HomePageState();
    }

    class _HomePageState extends State<HomePage> {

        static int deviceCode = 0;
        static double deviceCode = get_temp_globally();


        @override
        Widget build(BuildContext context) {
        final tempContext = TemperatureContext.of(context);
        Widget output = new MaterialApp(
            home: new Scaffold(
                appBar: new AppBar(
                title: new Text('Homepage'),
            ),
            body: new Column(
              children: <Widget>[
                new_widget(tempContext.deviceCode, tempContext.temperature),
                new FlatButton(
                    child: new Text('Set new sensor'),
                    onPressed: () {
                      Navigator.of(context).pushNamed('/ChangePage');
                    })
              ],
            )));
    return output;
  }

接下来是更改页面窗口小部件,当用户按下主页中的按钮时,会将用户转到该页面

class SensorWidget extends StatefulWidget {
  SensorWidget({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _SensorWidgetState createState() => new _SensorWidgetState();

}

class _SensorWidgetState extends State<SensorWidget> {
  static int deviceCode  = 0;
  static double temperature  = get_temp_globally();

  /* Some code that gets the deviceCode,
  temperature and sets them to the above
  variables */

  @override
  Widget build(BuildContext context) {
    output = TemperatureContext(
      deviceCode,
      temperature,
      child: MaterialApp(
        home: new Scaffold(
          appBar: new AppBar(
            title: const Text('Sensor widget'),
            actions: _buildActionButtons(),
          ),
          floatingActionButton: _buildScanningButton(),
          body: new Container(
            child: new FlatButton(
              child: new Text("Go back"),
              onPressed: () {
              Navigator.of(context).pop(true);
              }
            ),
          ),

        ),
        ),
      );
      return output;
  }
}

这是我的main.dart文件

void main() {
  runApp(new MyApp());

}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Temperature detector',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new HomePage(),
      routes: <String, WidgetBuilder> {
        '/HomePage' : (BuildContext context) => new HomePage(),
        '/SensorWidget': (BuildContext context) => new SensorWidget(),
      },
    );
  }
}

基本上,当我将new_widget函数放在我的HomePage类中时(我没有放在这里,但基本上是根据所提供的两个论点构建了一个小部件),我得到了“ NoSuchMethodError”:getter deviceCode在null上被调用。

我不明白为什么它为null,因为我已经对其进行了初始化。 有什么帮助吗? 谢谢

参见https://flutter.io/flutter-for-android/#what-is-the-equivalent-of-startactivityforresult

Navigator类处理Flutter中的路由,并用于从推入堆栈的路由中获取结果。 这是通过等待push()返回的Future来完成的。

例如,要启动允许用户选择其位置的位置路线,可以执行以下操作:

Map coordinates = await Navigator.of(context).pushNamed('/location');

然后,在您的位置路线内,一旦用户选择了他们的位置,您就可以弹出堆栈并显示结果:

Navigator.of(context).pop({"lat":43.821757,"long":-79.226392});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM