1

I am learning about StreamProviders and ChangeNotifierProvider and how to use them in a flutter app.

The problem I am having is when I create the StreamProvider in main.dart. I am getting this error Instance member 'getAgencyTrxn' can't be accessed using static access. (Documentation) as designated by a red line under getAgencyTrxn(). I have been following a tutorial and also some posts here but none of them quite match what I am doing.

How do I fix this error?

Here is what I have so far:

main.dart

Widget build(BuildContext context) {
    Provider.debugCheckInvalidValueType = null;

    globals.newTrxn = true;

    return MultiProvider(
      providers: [
        ChangeNotifierProvider<TrxnProvider>(create: (context) => TrxnProvider()),

        StreamProvider<TrxnProvider>(
            create: (context) => TrxnProvider.getAgencyTrxn(),
            initialData: []),
      ],
      child: MaterialApp(
        debugShowCheckedModeBanner: false,
        home: LoginScreen(),
      ),
    );
  }

trxn_provider.dart

class TrxnProvider extends ChangeNotifier {
  final firestoreService = FirestoreService();

  String? _clientFName;
  String? _clientLName;

  // Getters
  String? get clientFName => _clientFName;
  String? get clientLName => _clientLName;

  // Setters
  changeclientFName(String value) {
    _clientFName = value;
    notifyListeners();
  }

  changeclientLName(String value) {
    _clientLName = value;
    notifyListeners();
  }

  loadValues(QueryDocumentSnapshot trxns) {
    _clientFName = trxns['clientFName'];
    _clientLName = trxns['clientLName'];
  }

  getAgencyTrxn() {
    return firestoreService.getAgencyTrxns();
  }

  saveTrxn() {
    if (globals.newTrxn == true) {
      _trxnId = uuId.v4();
      globals.newTrxn = false;
    }

    var newTrxn = Trxns(
        clientFName: clientFName,
        clientLName: clientLName);

    firestoreService.saveTrxn(newTrxn);
  }

  deleteTrxn(String trxnId) {
    firestoreService.deleteTrxn(trxnId);
  }
}

firestore_service.dart

class FirestoreService {
  FirebaseFirestore _db = FirebaseFirestore.instance;

Stream<QuerySnapshot> getAgencyTrxns() async* {
    yield* FirebaseFirestore.instance
        .collection('agency').doc(globals.agencyId)
        .collection('trxns')
        .where('trxnStatus', isNotEqualTo: 'Closed')
        .snapshots();
  }
}

1 Answer 1

1

I found the solution. I needed to change this

create: (context) => TrxnProvider.getAgencyTrxn()

to this

create: (context) => TrxnProvider().getAgencyTrxn()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.