skip to Main Content

I’m trying to use _showWindow function but I’m getting

A value of type ‘Object’ can’t be assigned to a variable of type
‘List’

error.

 void _addBook(BuildContext context) async {
    List<dynamic> result = await _showWindow(context, "Enter the book name") ?? [];

  }

_showWindow function:

Future<String?> _showWindow(BuildContext context, String title) {
    return showDialog<String>(
        context: context,
        builder: (context) {
          String result = "";
          return AlertDialog(
            title: Text(title),
            content: TextField(
              keyboardType: TextInputType.text,
              onChanged: (String inputText) {
                result = inputText;
              },
            ),
            actions: <Widget>[
              TextButton(
                  onPressed: () {
                    Navigator.pop(context, "");
                  },
                  child: Text("Cancel")),
              TextButton(
                  onPressed: () {
                    Navigator.pop(context, result.trim());
                  },
                  child: Text("Approve"))
            ],
          );
        });
  }

How can I solve my problem? Thank you.

3

Answers


  1. showDialog is not a list type, you will need to update your whole logic and code structure assuming what you want to do is keep a list of all the Books entered from the Dialog.

    Login or Signup to reply.
  2. _showWindow returns String? not List<dynamic>.

    Here’s one way to handle this:

    final _books = List<String>.empty(growable: true);
    ...
    void _addBook(BuildContext context) async {
      final result = await _showWindow(context, "Enter the book name");
      if (result != null) {
        _books.add(result);
      }
    }
    

    }

    Login or Signup to reply.
  3. **You need to return the List of Object you can’t return the Widget in the Future<String?> U Need To return List in _showWindow Please Modify the code **

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search