Load Data from Firestore instead of JSON in card selector widget

Viewed 25

With the reference to this tutorial Card Selector Widget i am trying to load the data from firestore instead of Json file.

Here is the code for getting the data from firestore.

late DocumentSnapshot snapshot;
List? _cards;
Map? _data;
List<Map<String, dynamic>> accountList = [];

getData() async{
    final dataDB = await FirebaseFirestore.instance.collection('UserData').doc(getUid()).get();
    snapshot = dataDB;
    final Map<String, dynamic> documentData = snapshot.data() as Map<String, dynamic>;
    accountList = (documentData['accounts'] as List).map((accountList) => accountList as Map<String, dynamic>).toList();
  }

Here is the code where i am trying to add the data in the card.

@override
  void initState() {
    super.initState();
    getData();
    // DefaultAssetBundle.of(context).loadString(accountList).then((d) {
    //   _cards = json.decode(d);
    //   print('Card: $_cards');
    //   setState(() => _data = _cards![0]);
    // });
    _cards = accountList;
    _data = _cards![0];
    print('Datac: $_data');

    controller.addListener(() {
      setState(() {
        closeTopContainer = controller.offset > 0;
        openAppBar = controller.offset < 50;
      });
    });
  }

below is the error message i got,

RangeError (index): Invalid value: Valid value range is empty: 0

can anyone help me on this please.

Thanks in advance.

0 Answers
Related