6

I'm trying to send a element to one view. This element will be default element in the dropdown of the second page . I use something like that in first page

  onTap: () {
                        Navigator.push(
                            context,
                            new MaterialPageRoute(
                                builder: (context) => new SummaryPage(,
                                    dropdownItem: dropdownSelection)));
                      } 

Then in page two i create future to get all elements in dropdown and initstate method to set default element dropdown

 List data = List();

 Future<Null> getEstates() async {
    final response = await http
        .get('URL'
    }).catchError((error) {
      print(error.toString());
    });
    final responseJson = json.decode(response.body);
    final getBody = responseJson['body'];
    setState(() {
      data = getBody;
      loading = false;
    });
  }


 void initState() {
    super.initState();     

     setState(() {
          dropdownSelection = widget.dropdownItem.toString();
        });

getEstate return this

[{id: 1, descripcion: Terreno Rio}, {id: 2, descripcion: Terreno Asier}]

The dropdown look like similar to this

child: ButtonTheme(
                    alignedDropdown: true,
                    child: new DropdownButton<String>(
                      isDense: true,
                      hint: new Text("Search...",
                          textAlign: TextAlign.center),
                      value: dropdownSelection,
                      onChanged: (String newValue) {
                        setState(() {
                          dropdownSelection = newValue;
                      },
                      items: data.map((item) {
                        return new DropdownMenuItem<String>(
                          child: new Text(
                            item['description'],
                          ),
                          value: item['id'].toString(),
                        );
                      }).toList(),
                    ),
                  ),

The error that shows is

value == null || items.where((DropdownMenuItem item) => item.value == value).length == 1': is not true

Apparently code is fine but when i go to the second view shows a error view around 1 seconds and then show the second page. I think the value maybe load too soon and dropdown can´t set properly. Any idea why this error happens and how can i fix them?

2 Answers 2

9

That's because at the first time you don't have any value (your list is empty), so you can display a CircleProgressIndicator, like this:

        child: data.length > 0 ? ButtonTheme(
                        alignedDropdown: true,
                        child: new DropdownButton<String>(
                          isDense: true,
                          hint: new Text("Buscar..",
                              textAlign: TextAlign.center),
                          value: dropdownSelection,
                          onChanged: (String newValue) {
                            setState(() {
                              dropdownSelection = newValue;              
                          },
                          items: data.map((item) {
                            return new DropdownMenuItem<String>(
                              child: new Text(
                                'Finca: ' + item['descripcion'],
                              ),
                              value: item['id'].toString(),
                            );
                          }).toList(),
                        ),
                      ),
                    ) : Center(child: CircularProgressIndicator()),
Sign up to request clarification or add additional context in comments.

Comments

0

There could be 2 things wrong, that i can think of with your code:

  1. the value of DropdownMenutItem item is not unique, ie. some "id"s are repeated.
  2. the id that you are passing to PageTwo is not matching with any of the ids in the menu.

I would suggest using int id only instead of String as value

1 Comment

I can´t find this error. If i pass dropdownSelection = "1" : items.where((DropdownMenuItem<T> item) => item.value == value).length == 1': is not true.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.