收到过多的位置参数错误

时间:2019-10-14 01:24:18

标签: flutter dart

位置参数如何工作?

children: <Widget>[
  Table(
    children: [
      _buildTableRow(
        'one',
        'two',
        'Three'
      ),
    ],
  ),
],

我收到一个错误消息,说_buildTableRow的位置参数过多。

TableRow _buildTableRow(String listOfNames) {
  return TableRow(
    children: listOfNames.split(',').map((name) {
      return Container(
        child: Text(name)),
      );
    }).toList(),
  );
}

2 个答案:

答案 0 :(得分:1)

_buildTableRow(String listOfNames) 

仅接受一个字符串
并在您的代码中
您传递了三个字符串

_buildTableRow(
    'one',
    'two',
    'Three'
  ),

你可以做到

  _buildTableRow("one, two, Three")

完整代码

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: BasicTable(),
    );
  }
}


class BasicTable extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Table Widget")),
      body: Table(
        defaultColumnWidth: FixedColumnWidth(150.0),
        border: TableBorder(
          horizontalInside: BorderSide(
            color: Colors.black,
            style: BorderStyle.solid,
            width: 1.0,
          ),
          verticalInside: BorderSide(
            color: Colors.black,
            style: BorderStyle.solid,
            width: 1.0,
          ),
        ),
        children: [
          _buildTableRow("one, two, three"),
          _buildTableRow("Inducesmile.com, Google.com, Flutter.dev"),
          _buildTableRow("Inducesmile.com, Google.com, Flutter.dev"),
          _buildTableRow("Inducesmile.com, Google.com, Flutter.dev"),
        ],
      ),
    );
  }

  TableRow _buildTableRow(String listOfNames) {
    return TableRow(
      children: listOfNames.split(',').map((name) {
        return Container(
          alignment: Alignment.center,
          child: Text(name, style: TextStyle(fontSize: 20.0)),
          padding: EdgeInsets.all(8.0),
        );
      }).toList(),
    );
  }
}

enter image description here

答案 1 :(得分:0)

TableRow _buildTableRow(String listOfNames) {
  return TableRow(
    children: listOfNames.split(',').map((name) {
      return Container(child: Text(name));
    }).toList(),
  );
}

并像这样使用它:

_buildTableRow('one,two,Three')