How can I fetch data from dropbutton that depends on the value of two other drop buttons?

The problem is the fetchVersions() of the DropVersion is taking null values (taking null values of DropTitreand DropType when the interface is opened) instead of waiting that I select Titre and type then he receives the values of them,
so I tested my function in API and it works fine, but when I put the URL in the front the DropVersion becomes unselectable.

Code of DropTitre :

import 'dart:convert';

import 'package:firstparc/Models/CkTypesCks.dart';

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


class DropType extends StatefulWidget {
  final ValueChanged<String?> onTypeChanged;
 

  DropType({Key? key,required this.onTypeChanged }) : super(key: key);

  @override
  _DropTypeState createState() => _DropTypeState();
}

class _DropTypeState extends State<DropType> {
   String? selectedType;
  List<CkTypeCks> types = []; // Liste de titres
  

  @override
  void initState() {
    super.initState();
    fetchType(); // Appel à la fonction pour récupérer les titres
  }

  // Fonction pour récupérer les titres depuis l'API
  Future<void> fetchType() async {
    var url = Uri.parse('https://10.0.2.2:7116/api/CkTypeCks');
    try {
      http.Response response = await http.get(
        url,
        headers: <String, String>{
          'Content-Type': 'application/json; charset=UTF-8',
        },
      );

      if (response.statusCode == 200) {
        final data = jsonDecode(response.body) as List<dynamic>;
        setState(() {
          types = data.map((json) => CkTypeCks.fromJson(json)).toList();
        });
      }
    } catch (e) {
      print('Erreur lors de la récupération des titres : $e');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
       decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(5),
        border:Border.all(color: Colors.black),
        color: Colors.white,
      ),
      child: DropdownButtonFormField<CkTypeCks>(
        value: null, // La valeur sélectionnée initialement, null ou une valeur par défaut
        onChanged: (value) {
          setState(() {
            selectedType = value!.codeType.toString();
            
            print('Selected Type: ${selectedType}'); // Enregistrement de codeT dans selectedTitre
             widget.onTypeChanged(selectedType);
          });
         
        },
        items: types.map((CkTypeCks type) {
          return DropdownMenuItem<CkTypeCks>(
            value: type,
            child: Text(type.designation), // Affichage de la désignation dans le menu
          );
        }).toList(),
      ),
    );
  }
}



Code of DropType :

import 'dart:convert';

import 'package:firstparc/Models/CkTypesCks.dart';

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


class DropType extends StatefulWidget {
  final ValueChanged<String?> onTypeChanged;
 

  DropType({Key? key,required this.onTypeChanged }) : super(key: key);

  @override
  _DropTypeState createState() => _DropTypeState();
}

class _DropTypeState extends State<DropType> {
   String? selectedType;
  List<CkTypeCks> types = []; // Liste de titres
  

  @override
  void initState() {
    super.initState();
    fetchType(); // Appel à la fonction pour récupérer les titres
  }

  // Fonction pour récupérer les titres depuis l'API
  Future<void> fetchType() async {
    var url = Uri.parse('https://10.0.2.2:7116/api/CkTypeCks');
    try {
      http.Response response = await http.get(
        url,
        headers: <String, String>{
          'Content-Type': 'application/json; charset=UTF-8',
        },
      );

      if (response.statusCode == 200) {
        final data = jsonDecode(response.body) as List<dynamic>;
        setState(() {
          types = data.map((json) => CkTypeCks.fromJson(json)).toList();
        });
      }
    } catch (e) {
      print('Erreur lors de la récupération des titres : $e');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
       decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(5),
        border:Border.all(color: Colors.black),
        color: Colors.white,
      ),
      child: DropdownButtonFormField<CkTypeCks>(
        value: null, // La valeur sélectionnée initialement, null ou une valeur par défaut
        onChanged: (value) {
          setState(() {
            selectedType = value!.codeType.toString();
            
            print('Selected Type: ${selectedType}'); // Enregistrement de codeT dans selectedTitre
             widget.onTypeChanged(selectedType);
          });
         
        },
        items: types.map((CkTypeCks type) {
          return DropdownMenuItem<CkTypeCks>(
            value: type,
            child: Text(type.designation), // Affichage de la désignation dans le menu
          );
        }).toList(),
      ),
    );
  }
}

Code of DropVersion :

import 'dart:convert';
import 'package:firstparc/Models/CkEntete_fiche.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;


class DropVersion extends StatefulWidget {
  final ValueChanged<String?> onCodeChanged;
  
  final String? codeT;
  final String? codeType;

 
  DropVersion( {
    Key? key,
    
    this.codeT,
    this.codeType,
    required this.onCodeChanged,
  }) : super(key: key);

  @override
  DropVersionState createState() => DropVersionState();
}

class DropVersionState extends State<DropVersion> {
 
  List<CkEnteteFiche> versions = []; // Liste des versions

  @override
  void initState() {
    super.initState();
    // Écoute les changements de codeT et codeType et appelle fetchVersions lorsque les deux sont non nuls
   //if (widget.codeT != null && widget.codeType != null) {
    fetchVersions();
     
   //}
  }

  // Fonction pour récupérer les versions depuis l'API en fonction de codeT et codeType
  Future<void> fetchVersions() async {
   
    //var url = Uri.parse('https://localhost:7116/api/CkEnteteFiches/${widget.codeT}/${widget.codeType}');
    //var url = Uri.parse('https://10.0.2.2:7116/api/CkEnteteFiches/${widget.codeT}/${widget.codeType}');
    var url = Uri.parse('https://10.0.2.2:7116/api/CkEnteteFiches/1/1');
    print(url);

    try {
      http.Response response = await http.get(
        url,
        headers: <String, String>{
          'Content-Type': 'application/json; charset=UTF-8',
        },
      );

      if (response.statusCode == 200) {
        final data = jsonDecode(response.body) as List<dynamic>;
        print(response.body);
        setState(() {
          versions = data.map((json) => CkEnteteFiche.fromJson(json)).toList();
        });
      }
    } catch (e) {
      print('Erreur lors de la récupération des versions : $e');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
       decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(5),
        border:Border.all(color: Colors.black),
        color: Colors.white,
      ),
      child: DropdownButtonFormField<CkEnteteFiche>(
        value: null, // La valeur sélectionnée initialement, null ou une valeur par défaut
        onChanged: (value) {
          
             
            final selectedVersion = value!.version.toString();
            widget.onCodeChanged(selectedVersion);
            
            print('Selected Version: ${selectedVersion}'); 
          
          
        
        },
        
        items: versions.map((CkEnteteFiche version) {
          return DropdownMenuItem<CkEnteteFiche>(
            value: version,
            child: Text(version.version.toString()), // Affichage de la version dans le menu
          );
        }).toList(),
      ),
    );
  }
}

call in the interface code :

SizedBox(height: 24),
                      Text(
                        'Sélectionner un Titre :',
                        style: TextStyle(
                          fontSize: 18,
                          fontWeight: FontWeight.bold,
                          color: Color.fromARGB(255, 99, 151, 158),
                        ),
                      ),
                      DropTitre(
                        onTitreChanged: (titre) {
                          setState(() {
                            selectedTitre = titre;
                          });
                        },
                      ),
                      SizedBox(height: 24),
                      Text(
                        'Sélectionner un Type :',
                        style: TextStyle(
                          fontSize: 18,
                          fontWeight: FontWeight.bold,
                          color: Color(0xFF112F33),
                        ),
                      ),
                      DropType(
                        onTypeChanged: (type) {
                          setState(() {
                            selectedType = type;
                          });
                        },
                      ),
                      SizedBox(height: 24),
                      Text(
                        'Sélectionner une version :',
                        style: TextStyle(
                          fontSize: 18,
                          fontWeight: FontWeight.bold,
                          color: Color(0xFF112F33),
                        ),
                      ),
                      DropVersion(
                        codeT: selectedTitre,
                        codeType: selectedType,
                        onCodeChanged: (version) {
                          setState(() {
                            selectedVersion = version;
                          });
                        },
                      ),

New contributor

Ahmed Zouari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật