0
我需要幫助,我試圖按下按鈕時顯示一個新頁面。我得到以下錯誤:Dart Navigator給出NoSuchMethodError
I/flutter (31562): The following NoSuchMethodError was thrown while handling a gesture:
I/flutter (31562): Class 'MyHomePage' has no instance getter 'context'.
I/flutter (31562): Receiver: Instance of 'MyHomePage'
I/flutter (31562): Tried calling: context
I/flutter (31562): When the exception was thrown, this was the stack:
這裏是我的代碼:
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class MyHomePage extends StatelessWidget{
Container pictureSection(){
return new Container(
child: new Image.asset(
'images/sw.jpeg',
width: 600.0,
height: 240.0,
fit: BoxFit.cover,
),
);
}
Container mainMenuSection(){
var spacer = new SizedBox(height: 12.0);
return new Container(
child: new Column(
children: <Widget>[
spacer,
new RaisedButton(
onPressed:() {Navigator.of(context).pushNamed('/PlanetsPage');},
child: new Text('Get Planet'),
),
spacer,
],
),
);
}
@override
Widget build(BuildContext context){
var spacer = new SizedBox(height: 32.0);
var spacer2 = new SizedBox(height: 15.0);
return new Scaffold(
body: new Center(
child: new Column(
children: <Widget>[
pictureSection(),
mainMenuSection(),
],
),
),
);
}
}
class PlanetsPage extends StatelessWidget{
Widget build(BuildContext context){
return new Scaffold(
body: new Center(
child: new Column(
child: new Text('Hello World'),
),
),
);
}
}
void main(){
runApp(new MaterialApp(
home: new MyHomePage(),
routes: <String, WidgetBuilder> {
'/PlanetsPage': (BuildContext context) => new PlanetsPage(),
},
));
}
我想,當按下按鈕從主頁去行星頁面,並且錯誤只能說明當我按下按鈕時。我正在使用我的Hauwei Nova進行測試。