所以,我有一个简单的单页应用程序。我在该页上创建了一个复选框,以在单击复选框时切换到另一个页面。但是,我在复选框的onChanged参数中出现了一个错误。其内容如下:
Checkbox(
value: false, onChanged: (bool newValue) {
Navigator.push(
context,
new MaterialPageRoute(builder: (ctxt) => new SecondScreen()),
);
})但是最后一行代码给出了以下错误,我不知道如何解决它:
*
返回类型'SecondScreen‘不是闭包上下文所要求的小部件。
因此,SecondScreen不是一个无状态小部件。但是如何修改MaterialPageRouter以成功地转换到这个新页面。
我的第二个屏幕如下:
class SensorPage extends StatefulWidget {
const SensorPage({Key key, this.device}) : super(key: key);
final BluetoothDevice device;
@override
_SensorPageState createState() => _SensorPageState();
}
class SecondScreen extends State<SensorPage> {
final String SERVICE_UUID = "0000ffe0-0000-1000-8000-00805f9b34fb";
final String CHARACTERISTIC_UUID = "0000ffe1-0000-1000-8000-00805f9b34fb";
bool isReady;
Stream<List<int>> stream;
List<double> traceDust;
String _dataParser2(List<int> dataFromDevice) {
return utf8.decode(dataFromDevice);
}
@override
void initState() {
super.initState();
isReady = false;
}
@override
Widget build (BuildContext ctxt) {
Oscilloscope oscilloscope = Oscilloscope(
showYAxis: true,
padding: 0.0,
backgroundColor: Colors.black,
traceColor: Colors.white,
yAxisMax: 500.0,
yAxisMin: 0.0,
dataSet: traceDust,
);
return new Scaffold(
appBar: new AppBar(
title: new Text("Home"),
),
body: Container(
child: !isReady
? Center(
child: Text(
"Waiting...",
style: TextStyle(fontSize: 24, color: Colors.red),
),
)
: Container(
child: StreamBuilder<List<int>>(
stream: stream,
builder: (BuildContext context,
AsyncSnapshot<List<int>> snapshot) {
if (snapshot.hasError)
return Text('Error: ${snapshot.error}');
if (snapshot.connectionState ==
ConnectionState.active){
var currentValue2 = _dataParser2(snapshot.data);
traceDust.add(double.tryParse(currentValue2) ?? 0);
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expanded(flex: 1, child:Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Home',
style: TextStyle(fontSize: 14)),
Text('${currentValue2} IamHome',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 24))
]),
),
Expanded(
flex: 1,
child: oscilloscope,)
],
));
} else {
return Text('Check the stream');
}
},),
))
);
}
}发布于 2021-02-05 10:45:29
您使用的是小部件Navigator,应该在其中使用Navigator.of(context)。
您的代码应该如下所示:
Checkbox(
value: false, onChanged: (bool newValue) {
Navigator.of(context).push(MaterialPageRoute(
builder: (BuildContext context) =>
SecondScreen()));
})发布于 2021-02-05 11:10:00
在定义SensorPage的状态的类名时出现了一个错误。对于您的情况,应该是_SensorPageState
请查看下面的代码:
import 'package:flutter/material.dart';
// add the rest of packages
void main() {
runApp(MaterialApp(
home: MyApp()));
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
body: Container(
child: Checkbox(
value: false,
onChanged: (bool newValue) {
Navigator.push(
context,
new MaterialPageRoute(builder: (context) => new SensorPage()),
);
}
),
),
),
);
}
}
class SensorPage extends StatefulWidget {
const SensorPage({Key key, this.device}) : super(key: key);
final BluetoothDevice device;
@override
_SensorPageState createState() => _SensorPageState();
}
class _SensorPageState extends State<SensorPage> {
final String SERVICE_UUID = "0000ffe0-0000-1000-8000-00805f9b34fb";
final String CHARACTERISTIC_UUID = "0000ffe1-0000-1000-8000-00805f9b34fb";
bool isReady;
Stream<List<int>> stream;
List<double> traceDust;
String _dataParser2(List<int> dataFromDevice) {
return utf8.decode(dataFromDevice);
}
@override
void initState() {
super.initState();
isReady = false;
}
@override
Widget build (BuildContext ctxt) {
Oscilloscope oscilloscope = Oscilloscope(
showYAxis: true,
padding: 0.0,
backgroundColor: Colors.black,
traceColor: Colors.white,
yAxisMax: 500.0,
yAxisMin: 0.0,
dataSet: traceDust,
);
return new Scaffold(
appBar: new AppBar(
title: new Text("Home"),
),
body: Container(
child: !isReady
? Center(
child: Text(
"Waiting...",
style: TextStyle(fontSize: 24, color: Colors.red),
),
)
: Container(
child: StreamBuilder<List<int>>(
stream: stream,
builder: (BuildContext context,
AsyncSnapshot<List<int>> snapshot) {
if (snapshot.hasError)
return Text('Error: ${snapshot.error}');
if (snapshot.connectionState ==
ConnectionState.active){
var currentValue2 = _dataParser2(snapshot.data);
traceDust.add(double.tryParse(currentValue2) ?? 0);
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expanded(flex: 1, child:Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Home',
style: TextStyle(fontSize: 14)),
Text('${currentValue2} IamHome',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 24))
]),
),
Expanded(
flex: 1,
child: oscilloscope,)
],
));
} else {
return Text('Check the stream');
}
},),
))
);
}
}https://stackoverflow.com/questions/66061734
复制相似问题