SanjaySodani / multiselect_dropdown_flutter

A simple multiselect dropdown with select all and search options. Works with a simple list and a list of maps.
BSD 3-Clause "New" or "Revised" License
8 stars 4 forks source link

Can't get data with await #5

Open bubnenkoff opened 1 year ago

bubnenkoff commented 1 year ago
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'package:multiselect_dropdown_flutter/multiselect_dropdown_flutter.dart';

void main() {
  runApp(const MyApp());
}

class Controller extends GetxController {

  var names = <Map<String, dynamic>>[].obs;

  @override
  void onInit() {
    super.onInit();
    fetchDataFromNetwork();
  }

  Future<void> fetchDataFromNetwork() async {
    // Эмулируем задержку, как будто данные загружаются из сети
    await Future.delayed(Duration(seconds: 1));

    // Получаем данные (в данном случае, данные получаются с сервера)
    final List<Map<String, dynamic>> data = [
      {'id': 'dog', 'label': 'Dog'},
      {'id': 'cat', 'label': 'Cat'},
      {'id': 'mouse', 'label': 'Mouse'},
      {'id': 'rabbit', 'label': 'Rabbit'},
    ];

    names.assignAll(data);
  }
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: 'MultiSelect Dropdown demo',
      home: MultiSelectExample(),
    );
  }
}

class MultiSelectExample extends StatelessWidget {

  const MultiSelectExample({super.key});

  @override
  Widget build(BuildContext context) {

    final Controller ctrl = Get.put(Controller());

    return Scaffold(
      body: Padding(
        padding: const EdgeInsets.all(20),
        child: Column(
          children: [
            MultiSelectDropdown(
              list: ctrl.names,
              initiallySelected: const [],
              onChange: (newList) {
                // your logic
                // typically setting state
              },
              numberOfItemsLabelToShow: 2, // label to be shown for 2 items
              whenEmpty:
                  'Choose from the list', // text to show when selected list is empty
            ),
          ],
        ),
      ),
    );
  }
}

pubspec.yaml:

dependencies:
  flutter:
    sdk: flutter
  multiselect_dropdown_flutter: ^0.0.7
  get: ^5.0.0-release-candidate-4

result: can't select anything from dropdown like it have no any data.

bubnenkoff commented 1 year ago

it seems that widget do not check data update\changes

AkshayAvhad23 commented 1 month ago

Populating data from network, and dropdown shows no values or what so ever. Checked for state refresh as well. State refresh call is given and it's not showing any data.