簡體   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