【问题标题】:1 positional argument(s) expected, but 0 found. Try adding the missing arguments需要 1 个位置参数,但找到了 0 个。尝试添加缺少的参数
【发布时间】:2022-01-19 15:07:51
【问题描述】:

我有我一直在尝试处理的代码,但我不断收到此错误:

需要 1 个位置参数,但找到了 0 个。尝试添加缺少的 论据。

我在这个论坛上查看过类似的其他问题,但没有一个可以解决我的问题,因为我认为这个错误是我的代码/项目所特有的

并且错误出现在第 5 行到最后一行...从最后一行开始计算,在我有 ""child: Lists(//this is where the issue),""

import 'package:flutter/cupertino.dart';

import './widgets/todu_list.dart';
import './models/todus.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

void main() {
  runApp(MaterialApp(
    home: ItemList(),
  ));
}

class ItemList extends StatefulWidget {
  @override
  _ItemListState createState() => _ItemListState();
}

class _ItemListState extends State<ItemList> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      drawer: Drawer(
        child: IconButton(icon: Icon(Icons.add), onPressed: null),
      ),
      appBar: AppBar(
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.add),
            onPressed: () {},
          ),
        ],
        title: Text('Todu Scheduled Tasks'),
        centerTitle: true,
        backgroundColor: Colors.redAccent,
      ),
      body: SingleChildScrollView(
        child: Lists(),
      ),
    );
  }
}

然后在我有“Lists()”代码的文件上,bollow 代码就是文件内的内容:

import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';
import '../models/todus.dart';
import 'package:intl/intl.dart';
import 'package:sqflite/sqflite.dart';
import '../models/database_helper.dart';

class Lists extends StatefulWidget {
  final Function addTx;

  Lists(this.addTx);
  @override
  _ListsState createState() => _ListsState();
}

class _ListsState extends State<Lists> {
  final dbHelper = DatabaseHelper.instance;
  void _addNewTransaction(BuildContextcontext) {
    showModalBottomSheet(
      backgroundColor: Colors.white,
      isScrollControlled: true,
      shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.vertical(top: Radius.circular(25.0))),
      context: context,
      builder: (_) {
        return GestureDetector(
          onTap: () {},
          // Where i started the code pasting from
          child: Padding(
            padding: MediaQuery.of(context).viewInsets,
            child: Padding(
              padding: const EdgeInsets.all(8.0),
              child: Card(
                elevation: 0.000,
                child: Container(
                  padding: EdgeInsets.all(20),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.end,
                    mainAxisSize: MainAxisSize.min,
                    children: <Widget>[
                      TextField(
                        decoration: InputDecoration(labelText: 'Title'),
                        controller: _titleController,
                        autofocus: true,
                        onSubmitted: null,
                        // onChanged: (val) {
                        //   titleInput = val;
                        // },
                      ),
                      TextField(
                        decoration: InputDecoration(labelText: 'Description'),
                        controller: _discriptionController,
                        onSubmitted: null,
                        // onChanged: (val) => amountInput = val,
                      ),
                      Container(
                        height: 70,
                        child: Row(
                          children: [
                            Text(selectedDateAndTime == null
                                    ? 'No Date Choosen'
                                    : DateFormat('MM/dd/yyyy HH:mm')
                                        .format(selectedDateAndTime)
                                // : DateFormat.yMd()
                                //     .format(_selectedDate),
                                ),
                            FlatButton(
                              textColor: Theme.of(context).primaryColor,
                              child: Icon(Icons.calendar_today),
                              // onPressed: () async {
                              //   var value = await _selectedTime();
                              // },
                              onPressed: () => _selectDayAndTimeL(context),
                            ),
                          ],
                        ),
                      ),
                      RaisedButton(
                        child: Text('Save Todo'),
                        color: Theme.of(context).primaryColor,
                        textColor: Theme.of(context).textTheme.button.color,
                        onPressed: _submitData,
                      ),
                    ],
                  ),
                ),
              ),
            ),
          ),
        );
      },
    );
  }

  final _titleController = TextEditingController();
  final _discriptionController = TextEditingController();
  var favorite;
  // DateTime _selectedDate;
  DateTime selectedDateAndTime;
  @override
  void dispose() {
    super.dispose();
    _discriptionController.dispose();
    _titleController.dispose();
  }

  Future _selectDayAndTimeL(BuildContext context) async {
    DateTime _selectedDay = await showDatePicker(
        context: context,
        initialDate: DateTime.now(),
        firstDate: DateTime(2021),
        lastDate: DateTime(2030),
        builder: (BuildContext context, Widget child) => child);

    TimeOfDay _selectedTime = await showTimePicker(
      context: context,
      initialTime: TimeOfDay.now(),
    );

    if (_selectedDay != null && _selectedTime != null) {
      //a little check
    }
    setState(() {
      selectedDateAndTime = DateTime(
        _selectedDay.year,
        _selectedDay.month,
        _selectedDay.day,
        _selectedTime.hour,
        _selectedTime.minute,
      );
      // _selectedDate = _selectedDay;
    });
    // print('...');
  }

  List<ItemLists> items = [
    ItemLists(
      title: 'Best Music of the Year',
      description: 'Davido',
      favorite: false,
    ),
    ItemLists(
      title: 'Best Album Cover design',
      description: 'Brighter Press',
      favorite: false,
    ),
    ItemLists(
      title: 'Best Vocalist',
      description: 'Simi-Sola',
      favorite: false,
    ),
    ItemLists(
      title: 'Best Danced',
      description: 'Black Camaru',
      favorite: false,
    ),
    ItemLists(
      title: 'Best Performance',
      description: 'Shofeni-Were',
      favorite: false,
    ),
    ItemLists(
      title: 'Best Act',
      description: 'You Want to See Craze',
      favorite: false,
    ),
  ];

  void _submitData() {
    // if (_amountController.text.isEmpty) {
    //   return;
    // }
    final enteredTitle = _titleController.text;
    final enteredDescription = _discriptionController.text;

    if (enteredTitle.isEmpty) {
      return;
    }

    widget.addTx(
      enteredTitle,
      enteredDescription,
      selectedDateAndTime,
    );
    Navigator.of(context).pop();
  }

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      child: Container(
        child: ListView.builder(
          scrollDirection: Axis.vertical,
          shrinkWrap: true,
          itemBuilder: (context, index) {
            return Dismissible(
                key: ObjectKey(items[index]),
                background: Container(
                  color: Colors.red,
                ),
                child: Card(
                    child: ListTile(
                  leading: new IconButton(
                      icon: Icon(
                        Icons.check,
                        color:
                            items[index].favorite ? Colors.green : Colors.grey,
                      ),
                      tooltip: 'Add to Favorite',
                      onPressed: () {
                        setState(() {
                          items[index].favorite = !items[index].favorite;
                        });
                      }),
                  title: Text('${items[index].title}'),
                  subtitle: Text('${items[index].description}'),
                  trailing: IconButton(
                    icon: Icon(Icons.calendar_today),
                    onPressed: () => _selectDayAndTimeL(context),
                  ),
                )),
                onDismissed: (direction) {
                  final String myTitle = items[index].title;
                  // Remove the item from the data source.
                  setState(() {
                    var deletedItems = items.removeAt(index);
                    Scaffold.of(context).showSnackBar(
                      SnackBar(
                        content: Text('$myTitle Deleted'),
                        action: SnackBarAction(
                            label: 'Undo',
                            onPressed: () => setState(
                                  () => items.insert(index, deletedItems),
                                )),
                      ),
                    );
                  });
                });
          },
          itemCount: items.length,
        ),
      ),
    );
    floatingActionButton:
    FloatingActionButton(
      child: Icon(Icons.add),
      onPressed: () => _addNewTransaction(context),
      backgroundColor: Colors.redAccent,
    );
  }
}

你也可以查看我上传的图片,也许对你有帮助...谢谢

【问题讨论】:

  • 好吧,Lists(this.addTx); 期待一个函数,但你没有通过任何函数。 SingleChildScrollView( child: Lists(), ) 通过预期功能。
  • @kzrfaisal 当我在 Lists(addTx) 中添加 addTx 时,我仍然收到错误提示“未定义名称 'addTx' 尝试更正 ...”

标签: flutter dart flutter-layout dart-null-safety


【解决方案1】:

您的 Lists 类接受 1 个位置参数 - addTx。 所以,在调用代码时你只需要提供它。

您需要Lists(() {})Lists(yourFunctionDefinedSomewhereElse) 而不是Lists()

import 'package:flutter/cupertino.dart';

import './widgets/todu_list.dart';
import './models/todus.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

void main() {
  runApp(MaterialApp(
    home: ItemList(),
  ));
}

class ItemList extends StatefulWidget {
  @override
  _ItemListState createState() => _ItemListState();
}

class _ItemListState extends State<ItemList> {

  void likeAddTx() {
    print("Function like addTx param in Lists");
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      drawer: Drawer(
        child: IconButton(icon: Icon(Icons.add), onPressed: null),
      ),
      appBar: AppBar(
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.add),
            onPressed: () {},
          ),
        ],
        title: Text('Todu Scheduled Tasks'),
        centerTitle: true,
        backgroundColor: Colors.redAccent,
      ),
      body: SingleChildScrollView(
        child: Lists(likeAddTx),
      ),
    );
  }
}

【讨论】:

  • 我怎么称呼它?请帮助我...当我在 Lists(addTx) 中添加 addTx 时,我仍然收到错误消息“未定义的名称 'addTx' 尝试更正 ...”
  • addTx 是 Lists 类中定义的参数名称。 Lists 类的构造函数可以采用任何具有相同签名的函数作为其参数。我会更新我的答案
  • 我仍在学习过程中,但在理解构造函数、参数和 BuildContext 概念方面遇到了问题……但我在“Lists()”中添加了“addTx”,如下所示: Lists(addTx) 但仍然收到错误“Undifiend Name...”...请问我应该解决哪些代码应该放在哪里的问题?
  • 我已经更新了答案。只是问题中代码的解决方案。
  • 还是先学飞镖吧。没有任何框架(如颤振)。例如,在控制台应用程序中。
猜你喜欢
  • 2021-12-11
  • 2021-12-11
  • 2022-12-30
  • 2021-08-05
  • 2019-03-18
  • 1970-01-01
  • 2014-09-13
  • 1970-01-01
相关资源
最近更新 更多