Hi, I have a ChangeNotifierProvider<BoxProvider> above the MaterialApp that is consumed immediately so i use it's value to make a request. For whatever reason it keep returning null, Meanwhile it has the correct value after i have set it deep in a diff. route
class MyApp extends StatelessWidget {
final fireStoreService = MyFirestoreServices();
@override
Widget build(BuildContext context) {
final _currentUser = Provider.of<FirebaseUser>(context);
return MultiProvider(
providers: [
/// the annoying provider!
ChangeNotifierProvider<BoxProvider>(
builder: (context) => BoxProvider()),
/// a diff. provider that works fine
StreamProvider<Person>(
builder: (context) {
Stream<Person> _person;
if (_currentUser != null)
_person = fireStoreService.streamPerson(_currentUser);
return _person;
},
),
],
/// Both Providers consumed here
child: Consumer2<BoxProvider, Person>(
builder: (context, _boxPro, _person, _) {
if(_boxPro == null) return Text('Loading...');
return MultiProvider(
providers: [
StreamProvider<DocumentSnapshot>(
builder: (context) {
Stream<DocumentSnapshot> docSnapshot;
if (_person.hasBoxes)
docSnapshot = Firestore.instance
.collection('Boxes')
.document("${_currentUser.uid}")
.snapshots();
return docSnapshot;
},
),
StreamProvider<Box>(
builder: (context) {
Stream<Box> _boxStream;
if (_boxPro.boxPath != null)
_boxStream = Firestore.instance
/// the value (which is a document path) )is inserted here
.document(_boxPro.boxPath)
.snapshots()
.map((Box) => Box.Cloud(...));
return _boxStream;
},
),
],
child: MaterialApp(
home: Home(),
),
);
},
),
);
}
}
///setting the path to the selected box in a diff. route
return Consumer<BoxProvider>(
builder: (context, _boxPro, _) => GestureDetector(
onTap: () {
_boxPro.setBoxPath = boxes['boxPath'];
print("boxProPath: ${_boxPro.boxPath}"); /// returns the desired path.
},
),
class BoxProvider with ChangeNotifier {
String _boxPath;
String get boxPath => _boxPath;
set setBoxPath(String value) {
_boxPath = value;
notifyListeners();
}
}
Box is null when it is consumed which seems to imply the ChangeNotifierProvider<BoxProvider> returns a null.
Don't know how to go about it, please help!
Are you sure that it's not the Person that is null?
Very very sure. Person is the signed in user and i shouldn't be able to enter the app if it was null.
I've tried a couple of things since this post - i get the desired results when i print boxPath to the console from anywhere except inside MultiProvider between the said consumer Consumer2<BoxProvider, Person>(...) and MaterialApp.
ChangeNotifierProvider<BoxProvider> doesn't return null as i initially thought, I'm just unable to read from between the consumer and MaterialApp
Another interesting development, I have a few more similar ChangeNotifierProvider<T>(...) and they are all behaving in the same way - they all don't return any value between the consumer and the MaterialApp, they are not recognized at all. StreamProvider<Person>(... ) is working fine though. Maybe it has to do with how ChangeNotifierProvider<T>(...) works, you are the expert on that.
In the mean time, do you know of any workaround? Thanks
Hello! Sorry for the late reply.
To be honest I still don't really understand the problem. Do you mind making a minimal reproducible example (no firebase and other noise)?
Closing this since I cannot reproduce the issue