'FutureBuilder DropDownList selected Item not changing

This is a DropDownList inside A FutureBuilder, Make & Year are sent to the API and the Model list is returned as a stream.

The isssue is that the Model Dropdownlist is not updating the selected list value.

As you can see in the gif, the model dropdownlist is not changing the selected value

Is this even the correct way to approach this ???

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:towbars_and_more/services/api_manager.dart';

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  List products = [];
  String makeDropDownValue = '';
  String yearDropDownValue = '';
  Object modelDropDownValue = '';

  late Future future =  ApiManager().getModel(makeDropDownValue, yearDropDownValue);
  _getNewFuture(){
    setState(() {
      future = ApiManager().getModel(makeDropDownValue, yearDropDownValue);

    });
  }


  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
        title: const Text('Towbars And More'),
        actions: [
          IconButton(
              onPressed: () => Navigator.of(context).pushNamed('/cart'),
              icon: const Icon(Icons.shopping_cart)),
        ],
      ),
      body: Center(
        child: Form(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              //Make
              Padding(
                padding: const EdgeInsets.all(8.0),
                child: DropdownButton(
                  value: makeDropDownValue,
                  icon: const Icon(Icons.keyboard_arrow_down),
                  items: const <DropdownMenuItem<String>>[
                    DropdownMenuItem(
                      value: '',
                      child: Text('Make'),
                    ),
                    DropdownMenuItem(
                      value: 'ALFA ROMEO',
                      child: Text('Alfa Romeo'),
                    ),
                    DropdownMenuItem(
                      value: 'AUDI',
                      child: Text('Audi'),
                    ),
                    DropdownMenuItem(
                      value: 'Bmw',
                      child: Text('Bmw'),
                    ),
                    DropdownMenuItem(
                      value: 'CHEVROLET',
                      child: Text('Chevrolet'),
                    ),
                    DropdownMenuItem(
                      value: 'CHRYSLER',
                      child: Text('Chrysler'),
                    ),
                    DropdownMenuItem(
                      value: 'CITROEN',
                      child: Text('Citroen'),
                    ),
                    DropdownMenuItem(
                      value: 'DAEWOO',
                      child: Text('Daewoo'),
                    ),
                    DropdownMenuItem(
                      value: 'DAIHUTSU',
                      child: Text('Daihutsu'),
                    ),
                    DropdownMenuItem(
                      value: 'DODGE',
                      child: Text('Dodge'),
                    ),
                    DropdownMenuItem(
                      value: 'DS',
                      child: Text('DS'),
                    ),
                    DropdownMenuItem(
                      value: 'FIAT',
                      child: Text('Fiat'),
                    ),
                    DropdownMenuItem(
                      value: 'FORD',
                      child: Text('Ford'),
                    ),
                    DropdownMenuItem(
                      value: 'GWM',
                      child: Text('Gwm'),
                    ),
                    DropdownMenuItem(
                      value: 'HAVAL',
                      child: Text('Haval'),
                    ),
                    DropdownMenuItem(
                      value: 'HONDA',
                      child: Text('Honda'),
                    ),
                    DropdownMenuItem(
                      value: 'HYUNDAI',
                      child: Text('Hyundai'),
                    ),
                    DropdownMenuItem(
                      value: 'INFINITI',
                      child: Text('Infiniti'),
                    ),
                    DropdownMenuItem(
                      value: 'ISUZU',
                      child: Text('Isuzu'),
                    ),
                    DropdownMenuItem(
                      value: 'IVECO',
                      child: Text('Iveco'),
                    ),
                    DropdownMenuItem(
                      value: 'JAGUAR',
                      child: Text('Jaguar'),
                    ),
                    DropdownMenuItem(
                      value: 'JEEP',
                      child: Text('Jeep'),
                    ),
                    DropdownMenuItem(
                      value: 'KIA',
                      child: Text('Kia'),
                    ),
                    DropdownMenuItem(
                      value: 'LANCIA',
                      child: Text('Lancia'),
                    ),
                    DropdownMenuItem(
                      value: 'LAND ROVER',
                      child: Text('Land Rover'),
                    ),
                    DropdownMenuItem(
                      value: 'LEXUS',
                      child: Text('Lexus'),
                    ),
                    DropdownMenuItem(
                      value: 'MAHINDRA',
                      child: Text('Mahindra'),
                    ),
                    DropdownMenuItem(
                      value: 'MAZDA',
                      child: Text('Mazda'),
                    ),
                    DropdownMenuItem(
                      value: 'MERCEDES-BENZ',
                      child: Text('Mercedes-Benz'),
                    ),
                    DropdownMenuItem(
                      value: 'MINI',
                      child: Text('Mini'),
                    ),
                    DropdownMenuItem(
                      value: 'MITSUBISHI',
                      child: Text('Mitsubishi'),
                    ),
                    DropdownMenuItem(
                      value: 'NISSAN',
                      child: Text('Nissan'),
                    ),
                    DropdownMenuItem(
                      value: 'OPEL',
                      child: Text('Opel'),
                    ),
                    DropdownMenuItem(
                      value: 'PERGEOT',
                      child: Text('Pergeot'),
                    ),
                    DropdownMenuItem(
                      value: 'PORSCHE',
                      child: Text('Porsche'),
                    ),
                    DropdownMenuItem(
                      value: 'RENAULT',
                      child: Text('Renault'),
                    ),
                    DropdownMenuItem(
                      value: 'SAAB',
                      child: Text('Saab'),
                    ),
                    DropdownMenuItem(
                      value: 'SEAT',
                      child: Text('Seat'),
                    ),
                    DropdownMenuItem(
                      value: 'SKODA',
                      child: Text('Skoda'),
                    ),
                    DropdownMenuItem(
                      value: 'SSANGYONG',
                      child: Text('Ssangyong'),
                    ),
                    DropdownMenuItem(
                      value: 'SUBARU',
                      child: Text('Subaru'),
                    ),
                    DropdownMenuItem(
                      value: 'SUZUKI',
                      child: Text('Suzuki'),
                    ),
                    DropdownMenuItem(
                      value: 'TOYOTA',
                      child: Text('Toyota'),
                    ),
                    DropdownMenuItem(
                      value: 'VOLVO',
                      child: Text('Volvo'),
                    ),
                    DropdownMenuItem(
                      value: 'VW',
                      child: Text('VW'),
                    ),
                  ],
                  onChanged: (String? newValue) {
                    setState(() {
                      makeDropDownValue = newValue!;
                      _getNewFuture();
                    });
                  },
                ),
              ),
              //Year
              Padding(
                padding: const EdgeInsets.all(8.0),
                child: DropdownButton(
                  value: yearDropDownValue,
                  icon: const Icon(Icons.keyboard_arrow_down),
                  items: const <DropdownMenuItem<String>>[
                    DropdownMenuItem(
                      value: '',
                      child: Text('Year'),
                    ),
                    DropdownMenuItem(
                      value: '2022',
                      child: Text('2022'),
                    ),
                    DropdownMenuItem(
                      value: '2021',
                      child: Text('2021'),
                    ),
                    DropdownMenuItem(
                      value: '2020',
                      child: Text('2020'),
                    ),
                    DropdownMenuItem(
                      value: '2019',
                      child: Text('2019'),
                    ),
                    DropdownMenuItem(
                      value: '2018',
                      child: Text('2018'),
                    ),
                    DropdownMenuItem(
                      value: '2017',
                      child: Text('2017'),
                    ),
                    DropdownMenuItem(
                      value: '2016',
                      child: Text('2016'),
                    ),
                    DropdownMenuItem(
                      value: '2015',
                      child: Text('2015'),
                    ),
                    DropdownMenuItem(
                      value: '2014',
                      child: Text('2014'),
                    ),
                    DropdownMenuItem(
                      value: '2013',
                      child: Text('2013'),
                    ),
                    DropdownMenuItem(
                      value: '2012',
                      child: Text('2012'),
                    ),
                    DropdownMenuItem(
                      value: '2011',
                      child: Text('2011'),
                    ),
                    DropdownMenuItem(
                      value: '2010',
                      child: Text('2010'),
                    ),
                    DropdownMenuItem(
                      value: '2009',
                      child: Text('2009'),
                    ),
                    DropdownMenuItem(
                      value: '2008',
                      child: Text('2008'),
                    ),
                    DropdownMenuItem(
                      value: '2007',
                      child: Text('2007'),
                    ),
                    DropdownMenuItem(
                      value: '2006',
                      child: Text('2006'),
                    ),
                    DropdownMenuItem(
                      value: '2005',
                      child: Text('2005'),
                    ),
                    DropdownMenuItem(
                      value: '2004',
                      child: Text('2004'),
                    ),
                    DropdownMenuItem(
                      value: '2002',
                      child: Text('2002'),
                    ),
                    DropdownMenuItem(
                      value: '2001',
                      child: Text('2001'),
                    ),
                    DropdownMenuItem(
                      value: '1905',
                      child: Text('1905'),
                    ),
                  ],
                  onChanged: (String? newValue) {
                    setState(() {
                      yearDropDownValue = newValue!;
                      //TODO: NotifyListeneres
                      _getNewFuture();
                    });
                  },
                ),
              ),
              //Model
              if (makeDropDownValue != '' && yearDropDownValue != '')
                // map response into DropdownMenuItems
               FutureBuilder(
        future: future,
        builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot){
          List<Widget> children;
                  if (snapshot.hasData) {
                    modelDropDownValue = snapshot.data[0];
            children = <Widget>[
                      DropdownButton(
                        value: modelDropDownValue ,
                        isExpanded: true,
                        onChanged: (value) {
                          setState(() {
                            modelDropDownValue = value!;
                          });
                        },
                        items: snapshot.data.map<DropdownMenuItem<Object>>((item) {
                          return
                            DropdownMenuItem<Object>(
                            value: item,
                            child: Text(item.toString()),
                          );
                        }).toList(),
                      ),
                    ];
                  }
                  else if (snapshot.hasError) {
                    children = <Widget>[
                      const Icon(
                        Icons.error_outline,
                        color: Colors.red,
                        size: 60,
                      ),
                      Padding(
                        padding: const EdgeInsets.only(top: 16),
                        child: Text('Error: ${snapshot.error}'),
                      )
                    ];
                  }
                  else {
                  children = <Widget>[
                      const SizedBox(
                          width: 60,
                          height: 60,
                          child: CircularProgressIndicator())
                    ];
                  }
              ElevatedButton(
                  onPressed: (makeDropDownValue != '' && yearDropDownValue != '') ?
                      () => Navigator.of(context).pushNamed('/products') : null,
                  child: const Text('Search'));
                  return Center(
                    child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: children,
    ),);
              }
            ),
    ElevatedButton(
    onPressed: (makeDropDownValue != '' && yearDropDownValue != '') ?
    () => Navigator.of(context).pushNamed('/products') : null,
    child: const Text('Search'))
          ])
        ),
      ),
      drawer: Drawer(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            const Text('Contact Us'),
            IconButton(
                onPressed: () => Navigator.of(context).pushNamed('/contact'),
                icon: const Icon(Icons.contact_phone_outlined)),
            const Text('Logout'),
            IconButton(
                onPressed: () => FirebaseAuth.instance.signOut(),
                icon: const Icon(Icons.logout)),
          ],
        ),
      ),
    );
  }
}

And the API Manager Code

import 'dart:convert';

import 'package:http/http.dart' as http;


class ApiManager {
  Future<List> getModel(String make, String year) async {
    var headers = {'Cookie': 'Cookie_1=7e23553sadfaw45dfga342w5'};
    var request = http.MultipartRequest(
        'POST',
        Uri.parse(
            'https://www.brinkts.co.za/salesandroidapp/web/resources/v1/get_model.php'));
    request.fields.addAll({'brand': make, 'year': year});

    request.headers.addAll(headers);

    http.StreamedResponse response = await request.send();

    if (response.statusCode == 200) {
      // Convert response to usable List
      String stringData = await response.stream.bytesToString();
       List data = jsonDecode(stringData).cast<Map<String,dynamic>>();
       print(data);
    return data;
    } else {
      throw {
        response.reasonPhrase
      };
    }
  }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source