I'm a new flutter developer, and I have a problem with passing the provider through context.
If I understood correctly, the provider can be passed by context using:
Provider.of<DataModel>(context);
The error is: "Could not find the correct Provider above this PageMain Widget"
I am using the ChangeNotifierProvider, at the top of my widget tree, so any widget could access the DataModel class, with the intent of making it a sort of a singleton that acts as subject.
return ChangeNotifierProvider<DataModel>(
create: (context) => DataModel(),
builder: (context, dataModel){
return PageSplash();
}
Then, I have this function in the PageSplash(); Which is being called after an authentication of the user. In it I pass the context with the details of the user.
void _showNextPage(User user) {
PageShower.replaceWithMainPage(context, user, AppData.instance.user);
}
The replaceWithMainPage is a util function that is written like this:
static void replaceWithMainPage(BuildContext context, User user, RMUser receetMeUser) {
if (Platform.isAndroid) {
Navigator.of(context).pushReplacement(
MaterialPageRoute(
builder: (context) => PageMain(
firebaseUser: user,
receetMeUser: receetMeUser,
),
),
);
} else {
Navigator.of(context).pushReplacement(
CupertinoPageRoute(
builder: (context) => PageMain(
firebaseUser: user,
receetMeUser: receetMeUser,
),
),
);
}
}
I'm trying to access the provider that I injected in the page splash like this:
Provider.of<DataModel>(context);
DataModel code:
import 'package:flutter/material.dart';
class DataModel with ChangeNotifier{
bool _isLoading = true;
bool _isSearching = true;
String _version = "";
String _buildNumber = "";
set buildNumber(String value){
_buildNumber = value;
notifyListeners();
}
get buildNumber => _buildNumber;
set version(String value){
_version = value;
notifyListeners();
}
get version => _version;
set isLoading(bool value){
_isLoading = value;
notifyListeners();
}
get isLoading => _isLoading;
set isSearching(bool value){
_isSearching = value;
notifyListeners();
}
get isSearching => _isSearching;
}