DropdownMenuItem is not listing in Dropdown button

Viewed 36

I am trying to bring data from database to dropdown menu item.i had created a method to perform get method the api Is working fine I am storing the data in a list variable and making an attempt to call that variable inside dropdown menu item here data is storing in a list but I am not Abel to call that list I am calling api in initstate to load menu at start but it is not working following is my api where I am getting data

class ApiCalls extends ChangeNotifier {

  List vehicles = [];

  Future<void> getVehicles() async {
    final url = Ninecabsapi().urlHost + Ninecabsapi().getvehicle;
    try {
      var response = await http.get(
        Uri.parse(url),
        headers: {
          'Content-Type': 'application/json; charset=UTF-8',
        },
      );
      vehicleTypes = jsonDecode(response.body);
      if (vehicleTypes!['data'] != null) {
        vehicles = vehicleTypes!['data'];
      }

      notifyListeners();
    } catch (e) {
      print(e);
    }

  }

following is my widget where I want to view data

class CreatePackage extends StatefulWidget {



  @override
  State<CreatePackage> createState() => _CreatePackageState();
}

class _CreatePackageState extends State<CreatePackage> {
 @override
  void initState() {
    Provider.of<ApiCalls>(context, listen: false).getVehicles();
    // TODO: implement initState
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
       
        Padding(
          padding: const EdgeInsets.all(5.0),
          child: Consumer<ApiCalls>(
            builder: (context, value, child) {
              //print('this is from get vehicles response is:${value.vehicles}');
              return DropdownButton<String>(
                elevation: 16,
                isExpanded: true,
                hint: Text('please select type of vehicle'),
                items: value.vehicles.map((v) {
                  return DropdownMenuItem<String>(
                      onTap: () {
                        imageid = v['id'];
                        print(v['title']);
                      },
                      value: v['title'],
                      child: Text(v['title']));
                }).toList(),
                onChanged: (val) {
                  setState(() {
                    selectedValue = val!;
                  });
                },
                value: selectedValue,
              );
            },
          ),

following is list that I am Abel to print from variable this is the print statement that I am getting from list

I had noticed that the issue is coming from my notifier following is my main.dart

void main() {
  Provider.debugCheckInvalidValueType = null;
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    //  final auth = Auth();
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(
          create: (_) => Auth(),
        ),
        ProxyProvider<Auth, ApiCalls>(
            //create: (_) => ApiCalls(),
            update: (_, auth, __) => ApiCalls(auth.token)),
        //ApiCalls(auth.token)),
        ChangeNotifierProvider(
          create: (_) => FlutterFunctions(),
        ),
       
      ],

if I use

ChangeNotifierProxyProvider<Auth, ApiCalls>(
            create: (_) => ApiCalls(null),
            update: (context, auth, previous) => ApiCalls(auth.token)),

instead of proxy provider then it is working

0 Answers
Related