Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Positional arguments must occur before named arguments & Too many positional arguments:" error

Tags:

flutter

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)
          ),
          ]
          ),
          ),

        )
        ),
        );

  }
}
  1. 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)

  2. Positional arguments must occur before named arguments. Try moving all of the positional arguments before the named arguments.dart(positional_after_named_argument)

like image 383
odd_wolf Avatar asked Sep 17 '25 07:09

odd_wolf


2 Answers

Update (Dart 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');
}
like image 123
CopsOnRoad Avatar answered Sep 19 '25 21:09

CopsOnRoad


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.

like image 37
Jay Mungara Avatar answered Sep 19 '25 20:09

Jay Mungara