error converting an object into a List <MultiSelectItem<dynamic>>; and The getter 'length' was called on null

Viewed 41

In the function below, I am getting an error. If the error is clear, I do not know how to fix that problem. I am willing to get return, returning List <MultiSelectItem>.Many thanks in advance for your help.

ERROR : The getter 'length' was called on null. This is coming from package:multi_select_flutter/bottom_sheet/multi_select_bottom_sheet.dart

 @override
  void initState() {
    _selectedContext = _context;
    _selectedStatus = _status;
    getAllContextInFirebaseV1();
    super.initState();
  }
List <MultiSelectItem<dynamic>>allContexts;

Future <List<MultiSelectItem>> getAllContextInFirebaseV1() async {
    CollectionReference ref = FirebaseFirestore.instance.collection('Users')
        .doc((FirebaseAuth.instance.currentUser.uid))
        .collection('contexts');

    QuerySnapshot contextsQuery = await ref
        .get();

    final allContexts = contextsQuery.docs.map((snap) => snap.data()).toList();
    //allContexts.asMap();

    print(allContexts.asMap());
    return allContexts  ; //allContexts as Future<List<MultiSelectItem<dynamic>>> ; Does not work neither
  }
}

I have also tried this. If I do this, I am getting an other error.

The getter 'length' was called on null. Receiver: null Tried calling: length



//NOT WORKING PROPERLY - TO FIX
                  MultiSelectBottomSheetField(
                    key: _multiSelectKeyContext,
                    initialChildSize: 0.7,
                    maxChildSize: 0.95,
                    title: Text("Context", style: TextStyle(fontSize: 19),),
                    buttonText: Text(
                      "Context 66", style: TextStyle(fontSize: 19),),
                    searchTextStyle: TextStyle(fontSize: 19),
                    searchHintStyle: TextStyle(fontSize: 39),
                    itemsTextStyle: TextStyle(fontSize: 19),
                    items: allContexts,//getAllContextInFirebaseV1(),
                    searchable: true,
                    onConfirm: (valueContext) {
                      setState(() {
                        _selectedContext = valueContext;
                      });
                      _multiSelectKeyContext.currentState.validate();
                    },
                    chipDisplay: MultiSelectChipDisplay(
                      onTap: (item) {
                        setState(() {
                          _selectedContext.remove(item);
                        });
                        _multiSelectKeyContext.currentState.validate();
                      },
                    ),
                  ),

if I use this, below, I am getting this error:The argument type 'Future<List<MultiSelectItem>>' can't be assigned to the parameter type 'List<MultiSelectItem>'.

/NOT WORKING PROPERLY - TO FIX
                  MultiSelectBottomSheetField(
                    key: _multiSelectKeyContext,
                    initialChildSize: 0.7,
                    maxChildSize: 0.95,
                    title: Text("Context", style: TextStyle(fontSize: 19),),
                    buttonText: Text(
                      "Context 66", style: TextStyle(fontSize: 19),),
                    searchTextStyle: TextStyle(fontSize: 19),
                    searchHintStyle: TextStyle(fontSize: 39),
                    itemsTextStyle: TextStyle(fontSize: 19),
                    items: getAllContextInFirebaseV1(),
                    searchable: true,
                    onConfirm: (valueContext) {
                      setState(() {
                        _selectedContext = valueContext;
                      });
                      _multiSelectKeyContext.currentState.validate();
                    },
                    chipDisplay: MultiSelectChipDisplay(
                      onTap: (item) {
                        setState(() {
                          _selectedContext.remove(item);
                        });
                        _multiSelectKeyContext.currentState.validate();
                      },
                    ),
                  ),

1 Answers

The problem is return types, you need to change the return types from Firebase and also that of the function and all will be fine. So you can have the following:

Future<List<dynamic>> getAllContextInFirebaseV1() async {
    CollectionReference ref = FirebaseFirestore.instance.collection('Users')
        .doc((FirebaseAuth.instance.currentUser.uid))
        .collection('contexts');

    QuerySnapshot contextsQuery = await ref.get();

    final allContexts = contextsQuery.docs
      .map((snap) => (snap.data() as Map<String, dynamic>)['context_Name'])
      .toList() as List<dynamic>;

    print(allContexts);
    return allContexts;
  }
}
Related