Can someone please tell me how to correct these two errors? I tried several different approaches but wasn't successful.
Both issues are regarding the "class SecondScreen".
I don't understand where should I make the changes. This might be a very simple question. But I'd be glad if one of you could give me some insight on this issue.
I went through some sample codes & tutorials but couldn't find an effective one.
import 'package:flutter/material.dart';
//import 'package:flutter/services.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'firstapp',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.teal
),
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
List data = ["one", "Two", "Three", "Four", "Five"];
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView.builder(
itemCount: 5,
itemBuilder: (BuildContext context, int i) => Column(
children: <Widget>[
Container(
padding: EdgeInsets.symmetric(vertical: 12.0,horizontal: 10.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
child: FlatButton(
padding: EdgeInsets.symmetric(vertical: 12.0,horizontal: 10.0),
child: Text(data[i], style: TextStyle(fontWeight: FontWeight.bold,fontSize: 18.0)),
onPressed: () {
Navigator.push(
context,
new MaterialPageRoute(builder: (context) => new SecondScreen(i)),
);
},
)
),
Divider(color: Colors.black),
],
),
)
],),
)
);
}
}
class SecondScreen extends StatelessWidget {
List data = ["one", "Two", "Three", "Four", "Five"];
List data1 = ["this", "that", "here", "hello", "ahoy"];
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
//title: new Text("Second Screen"),
Container(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(top: 8.0, bottom: 6.0),
child: Text(data[i], style: TextStyle(fontWeight: FontWeight.bold,fontSize: 25.0)),
),
]
),
),
Padding(
padding: const EdgeInsets.only(top: 8.0, bottom: 2.0),
child: Row(children: <Widget>[
Text(data1[i],style: TextStyle(fontWeight: FontWeight.normal,fontSize: 12.0)
),
]
),
),
)
),
);
}
}
Too many positional arguments: 0 expected, but 1 found.
Try removing the extra positional arguments, or specifying the name for named arguments.dart(extra_positional_arguments_could_be_named)
Positional arguments must occur before named arguments.
Try moving all of the positional arguments before the named arguments.dart(positional_after_named_argument)
2.17
)You can now place named arguments before positional arguments. For example:
void main() {
foo('John', married: false); // Always worked
foo(married: true, 'Tony'); // Works now (with Dart 2.17)
}
void foo(String name, {bool? married}) {
print('Is $name married? $married');
}
Replace your code with as,
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'firstapp',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.teal
),
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
List data = ["one", "Two", "Three", "Four", "Five"];
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView.builder(
itemCount: 5,
itemBuilder: (BuildContext context, int i) =>
Column(
children: <Widget>[
Container(
padding: EdgeInsets.symmetric(
vertical: 12.0, horizontal: 10.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
child: FlatButton(
padding: EdgeInsets.symmetric(
vertical: 12.0, horizontal: 10.0),
child: Text(data[i], style: TextStyle(
fontWeight: FontWeight.bold, fontSize: 18.0)),
onPressed: () {
Navigator.push(
context,
new MaterialPageRoute(
builder: (context) => SecondScreen()),
);
},
)
),
Divider(color: Colors.black),
],
),
)
],),
)
);
}
}
class SecondScreen extends StatelessWidget {
List data = ["one", "Two", "Three", "Four", "Five"];
List data1 = ["this", "that", "here", "hello", "ahoy"];
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("Second Screen"),),
body: Container(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(top: 8.0, bottom: 6.0),
child: Text(data[i], style: TextStyle(
fontWeight: FontWeight.bold, fontSize: 25.0)),
),
]
),
),
),
);
}
}
In your listview.builder where you are using routing to navigate to page "SecondScreen(i)" you are passing "i" as parameter in constructor. But, inside the SecondScreen class you are not creating any constructor with such parameter is issue.
Another issue in your build method, you are using 2 Padding widgets to your body directly as a single child. you can use column widget to linearly add childs one by one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With