“位置参数必须在命名参数之前出现且位置参数太多:”错误

时间:2019-09-23 04:15:13

标签: android flutter

有人可以告诉我如何纠正这两个错误吗?我尝试了几种不同的方法,但没有成功。

两个问题都与“ SecondScreen类”有关。

我不知道应该在哪里进行更改。这可能是一个非常简单的问题。但我很高兴你们中的一个人能对这个问题给我一些见识。

我浏览了一些示例代码和教程,但是找不到有效的代码和教程。

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. 位置参数过多:预期为0,但找到1。 尝试删除多余的位置参数,或为命名为arguments.dart(extra_positional_arguments_could_be_named)

  2. 的名称指定名称
  3. 位置参数必须在命名参数之前出现。 尝试将所有位置参数移到命名的arguments.dart(positional_after_named_argument)

  4. 之前

1 个答案:

答案 0 :(得分:0)

用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)),
                  ),
                ]
            ),
          ),
        ),
    );
  }
}

在使用路由导航到页面“ SecondScreen(i)” 的listview.builder中,您将“ i”作为构造函数中的参数传递。但是,在 SecondScreen 类中,您没有使用该参数创建任何构造函数的问题。

构建方法中的另一个问题是,您正以2个孩子的身分直接使用2个填充控件。您可以使用列小部件来逐个线性添加子项。