At the moment I create an App where I display my sensor data in a List but my problem is that if I want to navigate to the List page I always get a Blackscreen. I tried to navigate like this:
Container(
height: 150.0,
margin: EdgeInsets.fromLTRB(75, 25, 75, 100),
child: Material(
borderRadius: BorderRadius.circular(20.0),
shadowColor: Colors.greenAccent,
color: Colors.green,
elevation: 7.0,
child: GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DataPage(key: null,)));
},
and I get the error
The argument type 'Null' can't be assigned to the parameter type 'Key'.
and that's part of my code for the List Page
class DataPage extends StatelessWidget {
const DataPage({
required Key key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Hive Test'),
),
body: Column(
children: <Widget>[
Expanded(child: _buildListView()),
],
));
}
with my current knowledge I don´t know how to solve my problem. Sorry for any grammar mistakes and I would be thankful for any help!
CodePudding user response:
It's happening because you're passing 'null' in Key while navigation to DataPage screen. Key is optional parameter, hence you can make it nullable this way.
Edits to your first snippet:
Container(
height: 150.0,
margin: EdgeInsets.fromLTRB(75, 25, 75, 100),
child: Material(
borderRadius: BorderRadius.circular(20.0),
shadowColor: Colors.greenAccent,
color: Colors.green,
elevation: 7.0,
child: GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DataPage()));
},
Edits to your second snippet:
class DataPage extends StatelessWidget {
const DataPage({
Key? key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Hive Test'),
),
body: Column(
children: <Widget>[
Expanded(child: _buildListView()),
],
));
}
CodePudding user response:
your constructor in data page like this :
const DataPage({Key? key}) : super(key: key);
