0
votes

The error received:

NoSuchMethodError: invalid member on null: 'collection'

I'm using the Flutter Provider Package 4.3.2+2 for complex state management.

I get my data from Firebase, this is the data structure:
data structure

This is the provider class:

class ClientsProvider extends ChangeNotifier {

  FirebaseFirestore _fs;
  StreamSubscription<QuerySnapshot> _stream;
  List<QueryDocumentSnapshot> clients = [];
  ClientsProvider() {
    _stream = _fs.collection("clients").snapshots().listen((event) {//THIS IS WHERE THE ERROR POINTS 
      clients = event.docs;
      notifyListeners();
    });
  }

  @override
  void dispose() {
    super.dispose();
    _stream.cancel();
  }
}

This is the parent widget:

class _ParentState extends State<Parent> {
  
  @override
  Widget build(BuildContext context) {
        return MultiProvider(
          providers: [
            Provider<ClientsProvider>(
                create: (context) => ClientsProvider()),
            ChangeNotifierProvider<MyProvider>(
                create: (context) => MyProvider()),
          ],
          child: Dashboard(),//This widget contains the Child() widget two widgets down. 
        );
  }
}

This is the child widget that needs to be updated when the firebase snapshot updates:

class _ChildState extends State<Child> {

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        _buildAddNew(),
        Consumer<ClientsProvider>(
          builder: (context, clientProvider, child) {
            return Container(child: Text(clientProvider.clients.toString()));
          },
        ),
      ],
    );
  }
}

Once again, the error received:

NoSuchMethodError: invalid member on null: 'collection'

The data isn't null, why am I receiving this error?

1

1 Answers

2
votes

_fs is null because you never assigned it. Perhaps you meant to do this, as shown in the documentation:

FirebaseFirestore _fs = FirebaseFirestore.instance;