简体   繁体   中英

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()), and am getting this error

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

I have gone through alot of similar questions here on StackOverFlow and realised i am supposed to add an argument inside the brackets "()" but i don't know which of the function from my Lists widget that i am expected to call there

Below is the "Lists" widget code

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

You have to give a function as parameter in order to build your widget. This is not a function of your widget that you will be calling but the function addTx that you will be calling from within your Lists widget.

Either remove the parameter or pass a function to solve it.

Example: since your function is expected to have 3 parameters:

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

you can create:

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

And you use this function Lists(addTitleDescDate)

As a side note I don't really see the point to have this function as a parameter shared to the Lists widget, but if you want to learn more about function as parameter that is still interesting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM