簡體   English   中英

Flutter:1 個位置參數,但發現 0

[英]Flutter:1 positional argument(s) expected, but 0 found

I am working on a flutter project, which separated the body: widget from the main.dart and placed it inside a new statefull widget with the file name todu_list.dart now i am trying to call it back to main.dart file body: SingleChildScrollView(child: Lists()),並收到此錯誤

1 positional argument(s) expected, but 0 found.
Try adding the missing arguments.

我在 StackOverFlow 上經歷了很多類似的問題,並意識到我應該在括號“()”內添加一個參數,但我不知道我的列表小部件中的哪個 function 我應該在那里調用

下面是“列表”小部件代碼

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

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

您必須提供 function 作為參數才能構建您的小部件。 這不是您將要調用的小部件的 function,而是您將從列表小部件中調用的 function addTx。

要么刪除參數,要么傳遞一個 function 來解決它。

示例:由於您的 function 預計有 3 個參數:

widget.addTx(
      enteredTitle,
      enteredDescription,
      selectedDateAndTime,
    );

您可以創建:

void addTitleDescDate(string title, string description, string date) { // NB you should probably use a Date object or epoch time.
   print(title);
   print(description);
   print(date);
}

你使用這個 function Lists(addTitleDescDate)

作為旁注,我真的不明白將這個 function 作為共享給 Lists 小部件的參數的意義,但是如果您想了解有關 function 作為參數的更多信息,那仍然很有趣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM