Setting Initial Dropdown Value Based on User Data

I’m building a Flutter app where users can edit their profile information. The profile form includes dropdowns for selecting the country and city. The list of countries is loaded into memory along with their respective cities. However, when a user edits their profile, I’m facing an issue with initializing the city dropdown with the user’s saved city.



class ProfilePage extends ConsumerStatefulWidget {
  const ProfilePage({Key? key}) : super(key: key);

  @override
  ConsumerState<ProfilePage> createState() => _ProfilePageState();
}

class _ProfilePageState extends ConsumerState<ProfilePage> {
  final _formKey = GlobalKey<FormBuilderState>();
  late AppUser? usr;
  List<String> cities = [];

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) async {
      await _loadInitialData();
    });
  }

  Future<void> _loadInitialData() async {
    final countries = ref.read(countriesProvider).countries;
    await ref.read(userProvider.notifier).getUserProfile().then((value) {
      usr = ref.read(userProvider.select((value) => value.user));
      final country = countries.byName(usr?.country ?? '');
      _updateCities(country, usr?.city);
      // Patch user data to the form
    });
  }

  void _updateCities(Country? country, [String? savedCity]) {
    if (country != null) {
      setState(() {
        cities = country.cities.names;
      });
      // Set initial city value
    }
  }

  @override
  Widget build(BuildContext context) {
    // other code
    CustomFormDropdown<String>(
              title: "Country",
              name: 'country',
              values: countries.names,
              onChanged: (value) {
                final country = countries.byName(value ?? '');
                _updateCities(country);
              },
            ),
    CustomFormDropdown<String>(
              title: "City",
              name: 'city',
              values: selectedCountry.cities.names,
            ),
            // Other form fields...

  }
}

The problem I’m facing is that the city dropdown doesn’t show the user’s saved city as the initial value. This is because the list of cities is initially empty and is fetched asynchronously after the user data is loaded.

I’ve tried updating the city list based on the selected country and setting the initial city value, but it doesn’t seem to work as expected.

How can I properly initialize the city dropdown with the user’s saved city after fetching the data? Any help or suggestions would be greatly appreciated. Thank you!

Image 1

Image 2

Image 1:
This is when I am edit the form. Here Country object contains List<City> objects. When user select any country Cities values get. Using this cities values we can choose cities from the dropdown menu.

Image 2:
In this scenario, when users view or update their profile information, the country and city names retrieved from the cloud are used to set the initial values of the dropdown menus. However, since the city values are initially empty, the initial city value fails to match with the dropdown values, resulting in it not being displayed.

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